0

I created a function calling a GUI having several entries and a button. If I push the button then the values on entries will be returned on the function.

def date_time_gui ():
    win = tk.Tk()
    win.title('powered by Python') # win title
    win.geometry('700x400') # size of win

    # add label on the win
    label = tk.Label(win, 
        text='type the heaving datetime and out water datetime',
        font = ('Arial Bold',25)
        )
    label.place(relx = 0.5, rely = 0.05, anchor = 'n')

    # label and entry for heaving datetime
    heaving_label = tk.Label(win,
        text='type heaving datetime',
        font = ('Arial Bold', 15)
        )
    heaving_label.place(relx = 0.1, rely = 0.3, anchor = 'w')

    h_year_label = tk.Label(win, text='YEAR (4 digits) :')
    h_year_label.place(relx = 0.1, rely = 0.4, anchor = 'w')
    h_year = tk.Entry(win, fg='black', width = 5)
    h_year.place(relx = 0.3, rely = 0.4, anchor = 'w')


    h_month_label = tk.Label(win, text='MONTH (2 digits) :')
    h_month_label.place(relx = 0.1, rely = 0.5, anchor = 'w')
    h_month = tk.Entry(win, fg='black', width = 5)
    h_month.place(relx = 0.3, rely = 0.5, anchor = 'w')


    h_date_label = tk.Label(win, text='DATE (2 digits) :')
    h_date_label.place(relx = 0.1, rely = 0.6, anchor = 'w')
    h_date = tk.Entry(win, fg='black', width = 5)
    h_date.place(relx = 0.3, rely = 0.6, anchor = 'w')


    h_hour_label = tk.Label(win, text='HOUR (24h, 2 digits) :')
    h_hour_label.place(relx = 0.1, rely = 0.7, anchor = 'w')
    h_hour = tk.Entry(win, fg='black', width = 5)
    h_hour.place(relx = 0.3, rely = 0.7, anchor = 'w')

    h_minute_label = tk.Label(win, text='MINUTE (2 digits) :')
    h_minute_label.place(relx = 0.1, rely = 0.8, anchor = 'w')
    h_minute = tk.Entry(win, fg='black', width = 5)
    h_minute.place(relx = 0.3, rely = 0.8, anchor = 'w')



    # label and entry for out water datetime
    outwater_label = tk.Label(win,
        text='type out water datetime',
        font = ('Arial Bold', 15)
        )
    outwater_label.place(relx = 0.5, rely = 0.3, anchor = 'w')

    o_year_label = tk.Label(win, text='YEAR (4 digits) :')
    o_year_label.place(relx = 0.5, rely = 0.4, anchor = 'w')
    o_year = tk.Entry(win, fg='black', width = 5)
    o_year.place(relx = 0.7, rely = 0.4, anchor = 'w')


    o_month_label = tk.Label(win, text='MONTH (2 digits) :')
    o_month_label.place(relx = 0.5, rely = 0.5, anchor = 'w')
    o_month = tk.Entry(win, fg='black', width = 5)
    o_month.place(relx = 0.7, rely = 0.5, anchor = 'w')


    o_date_label = tk.Label(win, text='DATE (2 digits) :')
    o_date_label.place(relx = 0.5, rely = 0.6, anchor = 'w')
    o_date = tk.Entry(win, fg='black', width = 5)
    o_date.place(relx = 0.7, rely = 0.6, anchor = 'w')


    o_hour_label = tk.Label(win, text='HOUR (24h, 2 digits) :')
    o_hour_label.place(relx = 0.5, rely = 0.7, anchor = 'w')
    o_hour = tk.Entry(win, fg='black', width = 5)
    o_hour.place(relx = 0.7, rely = 0.7, anchor = 'w')

    o_minute_label = tk.Label(win, text='MINUTE (2 digits) :')
    o_minute_label.place(relx = 0.5, rely = 0.8, anchor = 'w')
    o_minute = tk.Entry(win, fg='black', width = 5)
    o_minute.place(relx = 0.7, rely = 0.8, anchor = 'w')

    def run_func ():
        global heaving
        global outwater
        heaving = h_year.get()+'.'+h_month.get()+'.'+h_date.get()+' '+h_hour.get()+' '+h_minute.get()
        outwater = o_year.get()+'.'+o_month.get()+'.'+o_date.get()+' '+o_hour.get()+' '+o_minute.get()
        print('here')
        win.destroy()
        print('there')
        return heaving, outwater
    print('else')
    button = ttk.Button(win, text = 'run', command = run_func)  
    button.place(relx = 0.8, rely = 0.9, anchor = 'w')
    print('this')
    win.mainloop()
    print('where')
    return heaving, outwater

It works if I run it with the following code within the same script.

if __name__ == "__main__":
    h, o = date_time_gui()
    print(h,o)

it printed out h, o which are the return values of the function date_time_gui

However, if I apply this function to other definition like

def other_def():
    ## some code
    heaving_datetime, out_water_datetime = date_time_gui()

It just stops working.

I wanted to check which line in the date_time_gui is the problem, so I printed out 'there', 'where' etc.

When I run the other_def, it printed out else this here there

and then didn't print out 'where'

I really don't know what is the problem as the date_time_gui function itself works well. It doesn't work when I apply this to other functions.

  • maybe, to run the other_def I made a gui having checkbox to run the other_def. So, if I check the other_def function on the gui and hit the run button, the gui will be destroyed and other_def will be run. And the new gui for date_time_gui pop-up and hit the run button, then gui destroyed and stop working.
Kevin Mayo
  • 1,089
  • 6
  • 19
Dong-gyun Kim
  • 411
  • 5
  • 23
  • You said "it just stops working". What does that mean? Does the program crash? Do you get an exception? – Paul M. Sep 11 '20 at 15:50
  • if it doesn't print "where", this should mean, the `mainloop()` is still running and the GUI didn't exit. – Wups Sep 11 '20 at 15:55

1 Answers1

1

When using a tkinter window

  • Calling the loop() method activates the tkinter message pump and will stop the linear python code until the window is closed
  • You can exit the loop by manually closing the window or call quit() from your code.

In your case, calling win.mainloop() stopped the linear script.

You can exit the loop by adding win.quit() to the run_func function.

Try this code:

    def run_func ():
        global heaving
        global outwater
        heaving = h_year.get()+'.'+h_month.get()+'.'+h_date.get()+' '+h_hour.get()+' '+h_minute.get()
        outwater = o_year.get()+'.'+o_month.get()+'.'+o_date.get()+' '+o_hour.get()+' '+o_minute.get()
        print('here')
        win.destroy()
        print('there')
        win.quit()   # exit gui loop
        return heaving, outwater  # not needed
    
    print('else')
    button = ttk.Button(win, text = 'run', command = run_func)  
    button.place(relx = 0.8, rely = 0.9, anchor = 'w')
    print('this')
    win.mainloop()  # python code stops here until window closed
    print('where')
    heaving, outwater = 3,4  # for testing
    return heaving, outwater

if __name__ == "__main__":
    h, o = date_time_gui()
    print(h,o)

Output

else
this
here
there
where
3 4
Mike67
  • 11,175
  • 2
  • 7
  • 15