【发布时间】:2017-11-27 10:08:38
【问题描述】:
所以我有一个 tkinter 程序,它有 4 个变量标签。要获得其中一个标签,我需要将不同的变量除以 100,但是当我运行程序时,它等于 0。这是代码的一部分:eggzps = chookz / 100,这是整个代码:
import tkinter as tk
from PIL import Image, ImageTk
# --- functions ---
def moar_eggz():
global eggzps, chookz
chookz += 1
def update_labels():
try:
label1.config(text="Eggs: " + str(eggz))
label2.config(text="Eggs Per Second: " + str(eggzps))
label3.config(text="Egg Value: " + str(eggvalue))
label4.config(text=" Chickens: " + str(chookz))
except Exception as e:
print(e) # display exception to see problem
# repeat it after 20ms
root.after(20, update_labels)
def main_loop():
global eggz, eggzps
eggzps = chookz / 100
try:
eggz += eggzps
except Exception as e:
print(e) # display exception to see problem
# repeat it after 1000ms
root.after(1000, main_loop)
# --- main ---
root = tk.Tk()
root.title("Chicken Clicker")
eggz = 0
eggvalue = 0.2
chookz = 0
eggzps = 0
printeggzps = 0
# empty labels - `update_labels` will add text
label4 = tk.Label(root)
label3 = tk.Label(root)
label2 = tk.Label(root)
label1 = tk.Label(root)
label4.pack()
label3.pack()
label2.pack()
label1.pack()
imagecnv = Image.open("img\\1.png")
photo = ImageTk.PhotoImage(imagecnv)
openbutton = tk.Button(root, image=photo, width=500, height=500, command=moar_eggz)
openbutton.pack()
# run it first time at once
main_loop()
update_labels()
root.mainloop()
我尝试了很多东西,其中大部分都导致了我不得不在这里发布的错误,但是当错误修复时,问题并没有解决。我花了一整天的时间试图让它发挥作用,所以我只好问了。
任何建议或答案都将受到高度赞赏。
提前致谢!
【问题讨论】:
-
请提供一个仍然存在问题的最小示例!另外:您使用 GUI lib tkinter 真的相关吗?
-
你在划分之前打印
chookz确认,此时不是0? -
print()variables 看看你有什么价值 - 也许你做0/100然后你必须得到0 -
在 Python 3 中,
0/100返回0.0(浮点数),而不是0(整数)。
标签: python windows tkinter floating-point int