TonyIT
TonyIT

Reputation: 115

Data from child window

In the imported code, the variable 'values' is set correctly with the date selected by the user.

The def selection is called at the exit of the calendar.

I'm stuck. I don't know how to catch it and use it in my main code.

Thanks a lot.

# MAIN CODE (simplified)
from tkinter import *
import calendarWidget    
def manageCalendarWindow():
    root4 = Tk()
    data = {}
    app = calendarWidget.Calendar(root4, data)
    root4.mainloop()
manageCalendarWindow()
#how to get the date?

-.-.-.-.-.-.-.

# CALENDAR WIDGET (simplified)
class Calendar:
    def setup(self, y, m)
        (...)
        for w, week in enumerate(self.cal.monthdayscalendar(y, m), 2):
            for d, day in enumerate(week):
                if day:
                    b = tk.Button(self.parent, width=1, text=day, relief = 'flat',\
                    command=lambda day=day:self.selection(day, calendar.day_name[(day-1) % 7]))
                    self.wid.append(b)
                    b.grid(row=w, column=d)

    def selection(self, day, name):
        self.day_selected = day
        self.month_selected = self.month
        self.year_selected = self.year
        self.day_name = name

        #data
        self.values['day_selected'] = day
        self.values['month_selected'] = self.month
        self.values['year_selected'] = self.year
        self.values['day_name'] = name
        self.values['month_name'] = calendar.month_name[self.month_selected]

        self.setup(self.year, self.month)
        print(self.values) # <--- here the value is correct
        self.parent.destroy()

-.-.-.-.-.-.-.-

THIS WORKS:

def manageCalendarWindow():
    root4 = Tk()
    data = {}
    app = calendarWidget.Calendar(root4, data)
    root4.mainloop()
    return app    
app=manageCalendarWindow()
print(app.year_selected,app.month_selected,app.day_selected)

THIS NOT:

class enterWindows():
    def B_CalendarWindow(self):`
        app=self.manageCalendarWindow()
        print("year: ",app.year_selected)
        print("and... this will never be printed!")

    def manageCalendarWindow(self):
        root4 = Tk()
        data = {}
        app = calendarWidget.Calendar(root4, data)
        root4.mainloop()
        return app

Upvotes: 0

Views: 99

Answers (3)

TonyIT
TonyIT

Reputation: 115

As mentioned in my comments, there can be only one mainloop in the code. The others will be simply ignored. So it's not possible to use it to wait for a response from a child window.

The solution I used is

    app = myCalendar.Calendar(personal_path, root4, gui_language)
    root4.wait_window(app.parent)
    return app

The code opens the window and waits the result using wait_window().

Thanks all. Ciao.

Upvotes: 0

user4171906
user4171906

Reputation:

A simple proof-of concept program that gets a variable from a class instantiated within the class.

class SubClass():
    def __init__(self):
        self.variable="SubClass variable"

class MainClass():
    def __init__(self):
        app=self.instantiate_class()
        print(app.variable)  ## prints the string

    def instantiate_class(self):
        app=SubClass()
        print("app =", app)  ## not empty
        return app

MainClass()

Upvotes: 1

user4171906
user4171906

Reputation:

Everything local to the function manageCalendarWindow() is garbage collected when the function exits. This includes app (the class instance). You would have to return it to keep it alive. Note also that there is no self.month in the code you posted but I assume that comes from cutting the amount of code back for this post.

def manageCalendarWindow():
    root4 = Tk()
    data = {}
    app = calendarWidget.Calendar(root4, data)
    root4.mainloop()
    return app
    ## or
    ## return app.day_name
app=manageCalendarWindow()
print(app.day_name)
## or
##day_name=manageCalendarWindow()
##print(day_name)

Upvotes: 1

Related Questions