应用程序每隔4次运行一次? (蟒蛇)

时间:2017-11-27 03:51:46

标签: python tkinter

我制作了一个程序,并且由于没有充分的理由,除了第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()

我不知道是什么导致了它。

任何帮助都将受到高度赞赏!

提前致谢!

1 个答案:

答案 0 :(得分:1)

您不需要线程来运行它 - 您可以使用root.after(time, function)(而不是threadwhilesleep)每time运行一次功能毫秒。

您可以在update_labels()中运行moar_eggz() - 您无需在循环中运行它。但我保留它以显示如何使用两个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()

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