Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>here, try this:</p> <pre><code>from Tkinter import * import time root=Tk() variable=StringVar() def update_label(): i=0 while 1: i=i+1 variable.set(str(i)) root.update() your_label=Label(root,textvariable=variable) your_label.pack() start_button=Button(root,text="start",command=update_label) start_button.pack() root.mainloop() </code></pre> <p>That should give you a good example. However, it is important to note that during the while loop, you MUST call <code>root.update()</code> otherwise your GUI will freeze until the loop completes (in this case it <em>never</em> does) and never show your numbers.</p> <p>Also note that you <em>can</em> call <code>update_label()</code> from anywhere in your program. I just added it to the start button for example purposes.</p> <p>What was wrong with your code was that you had set the while loop free-floating and most importantly <em>before</em> your GUI's mainloop. When you do this, since this loop is infinate, it never allows <code>Tkinter</code> to start its <code>mainloop()</code>. However, if you were to put the while loop <em>after</em> the mainloop, then that would never be executed until <em>after</em> you exit the GUI, this is because the mainloop is infinate until it is stopped (closing the GUI).</p> <p>So to fix this you simply put it in a function and call it later on during <code>Tkinter</code>'s mainloop. You can do this various ways as well, for example, you can use <code>.after()</code> to perform a specific task after a certain amount of time, or make it the command of a button to be run when pressed, ect., ect. .</p> <p>However, The <em>proper</em> code you should use is the following, as you do not really want infinate loops in your code (other then you mainloop).:</p> <pre><code>class App (object): def __init__(self): self.root=Tk() self.variable=StringVar() self.i=0 self.your_label=Label(self.root,textvariable=self.variable) def grid(self): self.your_label.pack() def update_label(self): self.i=self.i+1 self.variable.set(str(self.i)) self.root.after(20,self.update_label) def run(self): self.grid() self.root.after(20,self.update_label) self.root.mainloop() if __name__=='__main__': App().run() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload