Tkinter:按下按钮更新标签的值

时间:2017-06-01 05:51:30

标签: python tkinter

每次按下特定按钮时,我都需要更新标签的值。但是root.update()不起作用,而且,我没有找到任何更新标签或至少整个gui表单来获取新值的信息!那么,任何人都可以帮忙吗? 这是代码:

from tkinter import *
import random
import decimal

# random numbers
random1 = decimal.Decimal(random.randrange(-155, 548))/100
random2 = decimal.Decimal(random.randrange(-155, 548))/100


root = Tk()
root.title("Cheking numbers")
root.geometry("640x480")
root.resizable(width=False, height=False)


# creating click event function
def ResultFunction():
    if random1 - random2 > 1.30:
        ResultLabel.configure(text='Result:' + str(random1 - random2))
    elif random1 < - 0.80 and random2 > -0.80:
        ResultLabel.configure(text='Result:' + str(random1))
    elif random1 > - 0.80 and random2 < -1.30:
        ResultLabel.configure(text='Result:' + str(random2))
    elif random1 < -0.80 and random2 < -1.30 and random1 < random2:
        ResultLabel.configure(text='Result:' + str(random1))
    elif random1 < -0.80 and random2 < -1.30 and random1 < random2:
        ResultLabel.configure(text='Result:' + str(random2))
    else:
        ResultLabel.configure(text='Result: 0')


# creating function for refreshing labels
def RefreshLabels():
    root.update()


# Labels
Number1Label = Label(root, text=str(random1), font='georgia 20')
Number1Label.pack()


Number2Label = Label(root, text=str(random2), font='georgia 20')
Number2Label.pack()


ResultLabel = Label(root, text='Result:', font='georgia 20')
ResultLabel.pack()


# Buttons
ResultButton = Button(root, text='Result', width=5, height=1, bg='white', fg='green', font='georgia 20', command=ResultFunction)
ResultButton.pack()


NextButton = Button(root, text='Next', width=6, height=1, bg='white', fg='green', font='georgia 20', command=RefreshLabels)
NextButton.pack()



mainloop()

1 个答案:

答案 0 :(得分:0)

每次单击ResultButton时,ResultLabel都会更新,但似乎没有任何内容发生,因为每次都会使用相同的随机值。所以你需要每次都生成一对新的随机数。

我简化了你的程序,并更改了名称以符合PEP-0008样式指南。我也用更明智的东西取代了可怕的“明星”导入。

此任务不需要高精度十进制数,简单的浮点数就足够了。

在ResultFunction中,最后两个elif子句具有完全相同的条件。我认为这是一个错字,所以我将第二个改为我认为你想要的。 ;)

import tkinter as tk
import random

root = tk.Tk()
root.title("Checking numbers")
root.geometry("320x200")
root.resizable(width=False, height=False)

def show_result():
    random1 = random.randrange(-155, 548) / 100
    random2 = random.randrange(-155, 548) / 100

    number1_label.configure(text=str(random1))
    number2_label.configure(text=str(random2))

    if random1 - random2 > 1.30:
        v = random1 - random2
    elif random1 < -0.80 < random2:
        v = random1
    elif random1 > -0.80 and random2 < -1.30:
        v = random2
    elif random1 < -0.80 and random2 < -1.30 and random1 < random2:
        v = random1
    elif random1 < -0.80 and random2 < -1.30 and random2 < random1:
        v = random2
    else:
        v = 0.0

    result_label.configure(text='Result: {:.2f}'.format(v))

font = 'georgia 20'

# Labels
number1_label = tk.Label(root, text='', font=font)
number1_label.pack()

number2_label = tk.Label(root, text='', font=font)
number2_label.pack()

result_label = tk.Label(root, text='', font=font)
result_label.pack()

# Buttons
result_button = tk.Button(root, text='Result', width=5, height=1, 
    bg='white', fg='green', font=font, command=show_result)
result_button.pack()

# Initialize
show_result()

root.mainloop()

此版本有两个按钮。 “下一步”按钮生成&amp;显示一对新的随机数,它还会清除“结果”标签。 “结果”按钮执行计算&amp;显示结果。

import tkinter as tk
import random

root = tk.Tk()
root.title("Checking numbers")
root.geometry("320x240")
root.resizable(width=False, height=False)

random1 = random2 = 0

# 'Next' button callback
def do_randoms():
    global random1, random2
    random1 = random.randrange(-155, 548) / 100
    random2 = random.randrange(-155, 548) / 100

    number1_label.configure(text=str(random1))
    number2_label.configure(text=str(random2))
    result_label.configure(text='Result:') 

# 'Result' button callback
def show_result():
    global random1, random2

    if random1 - random2 > 1.30:
        v = random1 - random2
    elif random1 < -0.80 < random2:
        v = random1
    elif random1 > -0.80 and random2 < -1.30:
        v = random2
    elif random1 < -0.80 and random2 < -1.30 and random1 < random2:
        v = random1
    elif random1 < -0.80 and random2 < -1.30 and random2 < random1:
        v = random2
    else:
        v = 0.0

    result_label.configure(text='Result: {:.2f}'.format(v))

font = 'georgia 20'

# Labels
number1_label = tk.Label(root, text='', font=font)
number1_label.pack()

number2_label = tk.Label(root, text='', font=font)
number2_label.pack()

result_label = tk.Label(root, text='Result:', font=font)
result_label.pack()

# Buttons
b = tk.Button(root, text='Next', width=6, height=1, 
    bg='white', fg='green', font=font, command=do_randoms)
b.pack()

b = tk.Button(root, text='Result', width=5, height=1, 
    bg='white', fg='green', font=font, command=show_result)
b.pack()

# Initialize
do_randoms()

root.mainloop()

我使用全局变量来存储随机数。最好将GUI包装在一个类中,并将随机数存储为GUI类实例的属性。