Python Forum
Using Tkinter inside function not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Tkinter inside function not working
#4
Code located after "root.mainloop()" is not going to execute until you close the root window. But you can't just move your while loop ahead of "root.mainloop()" because that prevents tkinter from running. What you need to do is simultaneously let mainloop() run while processing data from your serial port. There is probably a framework for doing this kind of thing built into your Raspberry Pi libraries. Look at the serial port library for some way to call a function when data is available on the serial port. Failing that, look for some kind of scheduler that will execute a function periodically.

You could always use the ".after" function from tkinter. In the example below I used root.after() in the random_rectangle() function to execute the function 10 times a second while letting mainloop() run at the same time.
import random
from tkinter import *
import time

rectangle = None
root = Tk() 
root.geometry("200x200")
 
canvas = Canvas( root, width = 1280, height = 800) 
canvas.pack(fill = "both", expand = True) 
canvas.create_image(0, 0, anchor = "nw")
     
def random_rectangle():
    global rectangle
    if rectangle is not None:
        canvas.delete(rectangle)
    x = random.randint(0, 200) // 50 * 50
    y = random.randint(0, 200) // 50 * 50
    rectangle = canvas.create_rectangle(x, y, x+50, y+50, fill='red')
    root.after(100, random_rectangle)
    
random_rectangle()

root.mainloop() 

print('This is not executed until you close the root window')
Reply


Messages In This Thread
RE: Using Tkinter inside function not working - by deanhystad - Dec-23-2020, 02:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter destroy label inside labelFrame Nick_tkinter 3 4,712 Sep-17-2023, 03:38 PM
Last Post: munirashraf9821
  [Tkinter] Help running a loop inside a tkinter frame Konstantin23 3 1,744 Aug-10-2023, 11:41 AM
Last Post: Konstantin23
  Tkinter won't run my simple function AthertonH 6 4,127 May-03-2022, 02:33 PM
Last Post: deanhystad
  tkinter toggle buttons not working Nu2Python 26 7,420 Jan-23-2022, 06:49 PM
Last Post: Nu2Python
  [Tkinter] tkinter best way to pass parameters to a function Pedroski55 3 5,056 Nov-17-2021, 03:21 AM
Last Post: deanhystad
  Creating a function interrupt button tkinter AnotherSam 2 5,709 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,171 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  [Tkinter] Redirecting all print statements from all functions inside a class to Tkinter Anan 1 2,746 Apr-24-2021, 08:57 AM
Last Post: ndc85430
  tkinter get function finndude 2 3,057 Mar-02-2021, 03:53 PM
Last Post: finndude
  tkinter -- after() method and return from function -- (python 3) Nick_tkinter 12 7,745 Feb-20-2021, 10:26 PM
Last Post: Nick_tkinter

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020