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
#5
(Dec-22-2020, 07:11 PM)Ensaimadeta Wrote: Hello iMuny, thanks for your suggestion.

I have done as you suggested and still no luck... Think

This is how it looks now:

from serial import *
from tkinter import *
import time

root = Tk() 
root.geometry("1280x800")
root.attributes('-fullscreen',True)
bg = PhotoImage(file = "/home/pi/Pictures/SCREEN2.png") 

canvas = Canvas( root, width = 1280, height = 800) 
canvas.pack(fill = "both", expand = True) 
canvas.create_image( 0, 0, image = bg, anchor = "nw") 

serialPort = "/dev/ttyUSB0"
ser = Serial(serialPort, 9600, timeout=0)
ser.flush()

def split_coords(astring):
    a, b = astring.split(';')
    a = int(a)
    b = int(b)
    
    return (a, b)

def paintRectangle1(canvas_ref):
    print("entered Rect 1")
    canvas_ref.create_rectangle(207, 151, 620, 374, fill='red')
    time.sleep(0.3)
    
def paintRectangle2(canvas_ref):
    print("entered Rect 2")
    canvas_ref.create_rectangle(600, 120, 800, 220, fill='red')
    time.sleep(0.3)
    
def paintRectangle3(canvas_ref):
    print("entered Rect 3")
    canvas_ref.create_rectangle(200, 260, 400, 360, fill='red')
    time.sleep(0.3)
    
def paintRectangle4(canvas_ref):
    print("entered Rect 4")
    canvas_ref.create_rectangle(600, 260, 800, 360, fill='red')
    time.sleep(0.3)

root.mainloop() 


while True:
    
    if ser.in_waiting> 0:
        line = ser.readline().decode('utf-8').rstrip()
        x, y = split_coords(line)
        print("x:", x, "- y:", y)
    
        canvas = Canvas( root, width = 1280, height = 800)
        
        if (x>19) and (x<60) and (y>20) and (y<40):
            print("OK button1")
            paintRectangle1(canvas)
        if (x>68) and (x<110) and (y>20) and (y<40):
            print("OK button2")
            paintRectangle2(canvas)
        if (x>19) and (x<60) and (y>45) and (y<80):
            print("OK button3")
            paintRectangle3(canvas)
        if (x>68) and (x<110) and (y>45) and (y<80):
            print("OK button4")
            paintRectangle4(canvas)
        '''    
        else:
            canvas.create_image( 0, 0, image = bg, anchor = "nw")

'''
and the error...

Error:
x: 11 - y: 11 Traceback (most recent call last): File "/home/pi/Documents/Touch-GUI-TKInter_4.py", line 55, in <module> canvas = Canvas( root, width = 1280, height = 800) File "/usr/lib/python3.7/tkinter/__init__.py", line 2405, in __init__ Widget.__init__(self, master, 'canvas', cnf, kw) File "/usr/lib/python3.7/tkinter/__init__.py", line 2299, in __init__ (widgetName, self._w) + extra + self._options(cnf)) _tkinter.TclError: can't invoke "canvas" command: application has been destroyed

same here. did you found the solution
Reply


Messages In This Thread
RE: Using Tkinter inside function not working - by darkv - Dec-03-2023, 09:41 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter destroy label inside labelFrame Nick_tkinter 3 4,594 Sep-17-2023, 03:38 PM
Last Post: munirashraf9821
  [Tkinter] Help running a loop inside a tkinter frame Konstantin23 3 1,622 Aug-10-2023, 11:41 AM
Last Post: Konstantin23
  Tkinter won't run my simple function AthertonH 6 3,955 May-03-2022, 02:33 PM
Last Post: deanhystad
  tkinter toggle buttons not working Nu2Python 26 7,176 Jan-23-2022, 06:49 PM
Last Post: Nu2Python
  [Tkinter] tkinter best way to pass parameters to a function Pedroski55 3 4,923 Nov-17-2021, 03:21 AM
Last Post: deanhystad
  Creating a function interrupt button tkinter AnotherSam 2 5,589 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,066 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,683 Apr-24-2021, 08:57 AM
Last Post: ndc85430
  tkinter get function finndude 2 2,994 Mar-02-2021, 03:53 PM
Last Post: finndude
  tkinter -- after() method and return from function -- (python 3) Nick_tkinter 12 7,528 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