Anonymous
Anonymous

Reputation: 375

Application only runs every 4th time? (python)

I made a program, and for no good reason, an error similar to this unexplained error is raised ever time except for the 4th:

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"

I cannot make sense of the error. If it helps, as it usually does, here is the code:

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()

I have no idea what may have caused it.

Any help given will be highly appreciated!

Thanks in advance!

Upvotes: 0

Views: 60

Answers (1)

furas
furas

Reputation: 142909

You don't need threads to run it - you can use root.after(time, function) (instead of thread, while and sleep) to run function every time milliseconds.

You could run update_labels() in moar_eggz() - you don't have to run it in loop. But I kept it to show how to use two 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()

BTW: you can also use update_labels() inside main_loop() and then you will need only one root.after()

Upvotes: 1

Related Questions