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.

  • No need to write a few questions in one question. - Xander

1 answer 1

On the first question:

You can see the value of an external variable without specifically indicating that it is external. But to change just like that is already impossible.

On the second question:

If you move Button, then you need to take care that there is a correct img variable, from where Button pulls the picture. And you, it seems, in that place there is a variable with the same name, but the wrong value.

UPD: Understood. In general, you simply use tkinter incorrectly. To create a dialog box, you need to use not the second Tk (), but Toplevel (). For buttons after creating them, you need to call the pack () method.

 root = Tk() def run(): dialog_window = Toplevel(root) img = PhotoImage(file="1.gif") b = Button(dialog_window, image=img) b.pack() dialog_window.mainloop() run() root.mainloop() 
  • Regarding the first question: Why can I change a button field, not to mention that it is external? Regarding the second: Unfortunately, the other img variable is not 100% there. The point is something else PS: I am on the site for the first time, how to transfer a line in the comments? - George
  • On the first question: because you do not change the value of the variable. When writing an element by index, you implicitly call the special __setattr__ method, which changes the field value. It is possible to call object methods from external scope. Even if they change something. - Xander
  • On the second question: then you need to see the whole code. On this piece nothing can be said. - Xander
  • from tkinter import * def run (root): dialog_window = Tk () img = PhotoImage (file = "images // home.gif") Button (dialog_window, image = img) .place (x = 0, y = 0) dialog_window .mainloop () run (Tk ()) I deliberately rendered the non-working part into a separate file, it also does not work. This is all its code. How to make a new line in the comments? - George
  • In the comments there is no new line. You can edit the question by adding the code there. - Xander