【问题标题】:Application only runs every 4th time? (python)应用程序仅每 4 次运行一次? (Python)
【发布时间】:2017-11-27 03:51:45
【问题描述】:

我做了一个程序,但无缘无故,除了第 4 次之外,每次都会引发类似于此无法解释的错误的错误:

Traceback (most recent call last):
  File "C:\Users\caleb.sim\3D Objects\Sim Inc\Applications\Games\Chicken Clicker\class1.py", line 54, in <module>
    openbutton = Button(root, image=photo, width = 500, height=500, command = moar_eggz)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2165, in __init__
    Widget.__init__(self, master, 'button', cnf, kw)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2095, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
TclError: unknown color name ".45051576"

我无法理解错误。如果它有帮助,就像通常那样,这里是代码:

from tkinter import *
from PIL import Image, ImageTk
import time, threading

root = Tk()
root.title("Chicken Clicker")
eggz = 0
eggvalue = 0.2
eggzps = 0
chookz = 0
def moar_eggz():
    global eggz, eggvalue, eggzps, chookz
    chookz = chookz + 1
    eggzps = chookz / 100
    printchookz = round(chookz)
def update_labels():
    try:
        while True:
            eggzLabel = "Eggs: " + str(eggz)
            eggzpsLabel = eggzps
            eggvalueLabel = "Egg Value: " + str(eggvalue)
            chookzLabel = " Chickens: " + str(chookz)
            label1.config (text=eggzLabel)
            label2.config (text=eggzpsLabel)
            label3.config (text=eggvalueLabel)
            label4.config (text=chookzLabel)
            time.sleep(0.2)
    except: pass
def main_loop():
    global eggz, printeggzps
    try:
        while True:
            global eggz, printeggzps
            eggz += printeggzps
            time.sleep(1)
    except: pass
eggzLabel = "Eggs: " + str(eggz)
eggzpsLabel = eggzps
eggvalueLabel = "Egg Value: " + str(eggvalue)
chookzLabel = " Chickens: " + str(chookz)
label4 = Label(root, text=eggzLabel)
label3 = Label(root, text=eggzLabel)
label2 = Label(root, text=eggzLabel)
label1 = Label(root, text=eggzLabel)
label4.pack()
label3.pack()
label2.pack()
label1.pack()
imagecnv = Image.open("img\\1.png")
photo = ImageTk.PhotoImage(imagecnv)
threading.Thread (target = main_loop).start()
threading.Thread (target = update_labels).start()

openbutton = Button(root, image=photo, width = 500, height=500, command = moar_eggz)
openbutton.pack()

root.mainloop()

我不知道是什么原因造成的。

我们将不胜感激!

提前致谢!

【问题讨论】:

  • 您能否提及您遇到此错误的操作系统。请看看这是否有帮助:bugzilla.redhat.com/show_bug.cgi?id=1043686
  • Tkinter 和线程相处得不是很好 - 你的线程运行 main_loop() 应该不是问题,因为它不会触及任何 Tkinter 对象,但 update_labels() 绝对是一个问题.使用.after() 安排未来的函数调用更有可能奏效。
  • 最好使用root.after(milliseconds, function)而不是线程,while循环和sleep()
  • 不要使用except:pass,你不会看到你有错误。至少使用except Exception as e: print(e)
  • 你可以在moar_eggz()中执行update_labels(),你不需要while和线程来运行这个函数。 printeggzps 是什么?它给出了错误。但是看不到,因为你用except:pass

标签: python tkinter


【解决方案1】:

你不需要线程来运行它——你可以使用root.after(time, function)(而不是threadwhilesleep)每time毫秒运行一次函数。

您可以在moar_eggz() 中运行update_labels() - 您不必循环运行它。但我保留它是为了展示如何使用两个root.after()

import tkinter as tk
from PIL import Image, ImageTk

# --- functions ---

def moar_eggz():
    global eggzps, chookz

    chookz += 1
    eggzps = chookz / 100

def update_labels():
    try:
        label1.config(text="Eggs: " + str(eggz))
        label2.config(text=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 100ms 
    root.after(100, update_labels)

def main_loop():
    global eggz

    try:
        eggz += printeggzps # ??? printeggzps didn't exist in original code
    except Exception as e:
        print(e) # display exception to see problem

    # repeat it after 100ms 
    root.after(100, main_loop)

# --- main ---

root = tk.Tk()
root.title("Chicken Clicker")

eggz = 0
eggvalue = 0.2
eggzps = 0
chookz = 0
# ??? printeggzps didn't exist in original code
printeggzps = 1

# 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()
# or run it first time after 100ms
#root.after(100, main_loop)
#root.after(100, update_labels)

root.mainloop()

顺便说一句:你也可以在main_loop() 中使用update_labels(),然后你只需要一个root.after()

【讨论】:

    猜你喜欢
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-07
    • 2020-07-05
    • 2018-03-12
    • 1970-01-01
    相关资源
    最近更新 更多