So, we have this situation:
from tkinter import * x=5 q=Button() def func(): x+=2 q["text"]="hello" func() Here we have an error, since x creates a local one here and we are trying to change a non-existent variable.
We can fix the situation:
from tkinter import * x=5 q=Button() def func(): global x x+=2 q["text"]="hello" func() There are no errors, everything works fine. We clarified that we are working with the global variable х . However, with the q button, we did not perform this operation. Then why is it visible? I have a hunch that objects are visible without global, but in Python everything is objects, even variables. How then does this happen?
The second question concerns the image on the button.
In this case, the button with the picture is normally created:
def run(root): dialog_window=Tk() img=PhotoImage(file="images//home.gif") Button(root,image=img).place(x=0,y=0) dialog_window.mainloop() run(Tk()) However, if you place a button on the dialog_window:
Button (root, image = img) .place (x = 0, y = 0)
That quits nothing and the interpreter will issue the following:
Traceback (most recent call last): File "D:\projects\Tk_editor\test.py", line 14, in <module> run(Tk()) File "D:\projects\Tk_editor\test.py", line 12, in run Button(dialog_window,image=img).place(x=0,y=0) File "C:\Users\Kastiel\AppData\Local\Programs\Python\Python35- 32\lib\tkinter\__init__.py", line 2209, in __init__ Widget.__init__(self, master, 'button', cnf, kw) File "C:\Users\Kastiel\AppData\Local\Programs\Python\Python35- 32\lib\tkinter\__init__.py", line 2139, in __init__ (widgetName, self._w) + extra + self._options(cnf)) _tkinter.TclError: image "pyimage1" doesn't exist What is the problem? And how to create this button on the second window? PS: The same button without a picture is normally created on the second window:
def run(root): dialog_window=Tk() Button(dialog_window).place(x=0,y=0) dialog_window.mainloop() run(Tk()) Please help me figure it out.