Serkan Senyuz Serkan Senyuz - 3 months ago 10
Python Question

Why don't these tkinter programs behave identically?

I have 2 codes I made. The 2nd one is based off of the first one. Unfortunately for some reason the 2nd one doesn't work even though the first one does.

First code:

import time
from Tkinter import *

root = Tk()
t=StringVar()
num=1
t.set(str(num)
thelabel = Label(root, textvariable=t).pack()

def printnum (x):
while x<= 100:
t.set(str(x))
x += 1
root.update()
time.sleep(30)

printnum(num)
root.mainloop()


This code works like a charm. Here is the other one.

Second code:

#!/usr/bin/python
# -*- coding: latin-1 -*-

import Adafruit_DHT as dht
import time
from Tkinter import *

root = Tk()
k=StringVar()
num = 1
k.set(str(num))
thelabel = Label(root, textvariable=k).pack
def printnum(x):
while x <= 10000000000000:
h,t = dht.read_retry(dht.DHT22, 4)
newtext = "Temp%s*C Humidity=%s" %(t,h)
k.set(newtext)
x += 1
root.update
time.sleep(30)

printnum(num)
root.mainloop()


The code runs but it doesn't do anything, no window pops up like the other code does. Please help I cannot figure out how to fix this. Or why the first one works and the second one doesn't.

Answer

root.update doesn't do anything. You need to add ():

root.update()

That being said, your algorithm is the wrong way to run periodic tasks in tkinter. See http://stackoverflow.com/a/37681471/7432