'clicker game images not updating
It seems that everything is working and even the images are working but instead of disappearing after the (price) or (popularity) gets too high for the image, the image seems to stay there and the images stack on each other. Another thing that seems to working is the images are updating as it goes on but it stacks on each other and when the price and popularity go down the images just stay there and don't fade away Here is the code: import tkinter, os from tkinter.ttk import Progressbar
price = 1000
popularity = 10
def update_ui():
price_fully_high_meter["value"] = price
popularity_fully_high_text.config(text="full popularity" + str(popularity))
popularity_fully_high_meter["value"] = popularity
price_fully_high_text.config(text="full price" + str(price))
if price >= 0 and price <= 10000 and popularity >=0 and popularity <20:
image_label1.config(image=resized_bitcoin_low)
bitcoin_condition.config(text="My value is rockbottom!!")
update_ui
elif price >= 11000 and price <= 20000 and popularity >= 25 and popularity <40:
image_label1.config(image=resized_bitcoin_medium)
bitcoin_condition.config(text="i am average right now")
update_ui
elif price >= 21000 and price <= 40000 and popularity >= 45 and popularity <60:
image_label1.config(image=resized_bitcoin_above_average)
bitcoin_condition.config(text="i am above average right now")
update_ui
elif price >= 41000 and price <= 50000 and popularity >= 65 and popularity <80:
image_label1.config(image=resized_bitcoin_high)
bitcoin_condition.config(text="I am very high")
update_ui
elif price >= 51000 and price <= 60000 and popularity >= 80 and popularity <=100:
image_label1.config(image=resized_bitcoin_moon)
bitcoin_condition.config(text="I am too the moon")
update_ui
def pop_btn_click():
global popularity
if popularity <= 95:
popularity +=5
print("popularity:" + str(popularity))
update_ui()
def raise_btn_click():
global price
if price <= 60000:
price += 1000
print("price:" + str(price))
update_ui()
def autoupdate():
global price
global popularity
if price > 0:
price -= 2000
print("price:" + str(price))
if popularity > 0:
popularity -= 5
print("popularity" + str(popularity))
def mastertimer():
autoupdate()
update_ui()
root.after(2000, mastertimer)
root = tkinter.Tk()
root.title("my virtual bitcoin")
root.geometry("300x450")
#add the starting pet image
img_path = os.path.dirname(__file__) + "\\bitcoin_low.png"
high_image = tkinter.PhotoImage(file=img_path)
resized_bitcoin_low = high_image.subsample(3,3)
image_label1 = tkinter.Label(root, image=resized_bitcoin_low)
image_label1.grid(row=0, column=0, columnspan=2)
img_path = os.path.dirname(__file__) + "\\bitcoin_high.png"
low_image1 = tkinter.PhotoImage(file=img_path)
resized_bitcoin_high = low_image1.subsample(3,3)
image_label1 = tkinter.Label(root, image=resized_bitcoin_high)
image_label1.grid(row=0, column=0, columnspan=2)
img_path = os.path.dirname(__file__) + "\\bitcoin_medium.png"
low_image1 = tkinter.PhotoImage(file=img_path)
resized_bitcoin_medium = low_image1.subsample(3,3)
image_label1 = tkinter.Label(root, image=resized_bitcoin_medium)
image_label1.grid(row=0, column=0, columnspan=2)
img_path = os.path.dirname(__file__) + "\\bitcoin_above_average.png"
low_image1 = tkinter.PhotoImage(file=img_path)
resized_bitcoin_above_average = low_image1.subsample(3,3)
image_label1 = tkinter.Label(root, image=resized_bitcoin_above_average)
image_label1.grid(row=0, column=0, columnspan=2)
img_path = os.path.dirname(__file__) + "\\bitcoin_moon.png"
low_image1 = tkinter.PhotoImage(file=img_path)
resized_bitcoin_moon = low_image1.subsample(3,3)
image_label1 = tkinter.Label(root, image=resized_bitcoin_moon)
image_label1.grid(row=0, column=0, columnspan=2)
#add label for pet condition
bitcoin_condition= tkinter.Label(text="Im low, feeling too lowww")
bitcoin_condition= tkinter.Label(text="Im high, feeling good and high")
bitcoin_condition.grid(row=1, column=0,columnspan=2)
raise_feed = tkinter. Button(root, text= "buy me !", command=raise_btn_click)
raise_feed.grid(row=2,column=0,)
#add price bar
price_fully_high_meter = Progressbar(root, orient= tkinter.HORIZONTAL, length=100, max=40000, mode = "determinate")
price_fully_high_meter["value"] = 0
price_fully_high_meter.grid(row=3,column=0,)
#progress bar text
price_fully_high_text = tkinter.Label(text="price : " + str(price))
price_fully_high_text.grid(row=4,column=0,)
raise1_feed = tkinter. Button(root, text= "post me !", command=pop_btn_click)
raise1_feed.grid(row=2,column=1,)
#add price bar
popularity_fully_high_meter = Progressbar(root, orient= tkinter.HORIZONTAL, length=100, max=100, mode = "determinate")
popularity_fully_high_meter["value"] = 0
popularity_fully_high_meter.grid(row=3,column=1,)
#progress bar text
popularity_fully_high_text = tkinter.Label(text="popularity: " + str(popularity))
popularity_fully_high_text.grid(row=4,column=1,)
mastertimer()
root.mainloop()
Solution 1:[1]
Seems to work for me....did you import time? I also used png files, with transparent backgrounds because I couldn't repro what you describe.
Edit: omitting time didn’t have any impact on mine; keeps working or I am missing what you are describing. ?????
import tkinter
import time
from tkinter import ttk
import os
price = 1000
popularity = 10
def update_ui():
price_fully_high_meter["value"] = price
popularity_fully_high_text.config(text="full popularity" + str(popularity))
popularity_fully_high_meter["value"] = popularity
price_fully_high_text.config(text="full price" + str(price))
if price >= 0 and price <= 10000 and popularity >=0 and popularity <20:
image_label1.config(image=resized_bitcoin_low)
bitcoin_condition.config(text="My value is rockbottom!!")
update_ui
elif price >= 11000 and price <= 20000 and popularity >= 25 and popularity <40:
image_label1.config(image=resized_bitcoin_medium)
bitcoin_condition.config(text="i am average right now")
update_ui
elif price >= 21000 and price <= 40000 and popularity >= 45 and popularity <60:
image_label1.config(image=resized_bitcoin_above_average)
bitcoin_condition.config(text="i am above average right now")
update_ui
elif price >= 41000 and price <= 50000 and popularity >= 65 and popularity <80:
image_label1.config(image=resized_bitcoin_high)
bitcoin_condition.config(text="I am very high")
update_ui
elif price >= 51000 and price <= 60000 and popularity >= 80 and popularity <=100:
image_label1.config(image=resized_bitcoin_moon)
bitcoin_condition.config(text="I am too the moon")
update_ui
def pop_btn_click():
global popularity
if popularity <= 95:
popularity +=5
print("popularity:" + str(popularity))
update_ui()
def raise_btn_click():
global price
if price <= 60000:
price += 1000
print("price:" + str(price))
update_ui()
def autoupdate():
global price
global popularity
if price > 0:
price -= 2000
print("price:" + str(price))
if popularity > 0:
popularity -= 5
print("popularity" + str(popularity))
def mastertimer():
autoupdate()
update_ui()
root.after(2000, mastertimer)
root = tkinter.Tk()
root.title("my virtual bitcoin")
root.geometry("300x450")
#add the starting pet image
img_path = os.path.dirname(__file__) + "\\bitcoin_low.png"
high_image = tkinter.PhotoImage(file=img_path)
resized_bitcoin_low = high_image.subsample(3,3)
image_label1 = tkinter.Label(root, image=resized_bitcoin_low)
image_label1.grid(row=0, column=0, columnspan=2)
img_path = os.path.dirname(__file__) + "\\bitcoin_high.png"
low_image1 = tkinter.PhotoImage(file=img_path)
resized_bitcoin_high = low_image1.subsample(3,3)
image_label1 = tkinter.Label(root, image=resized_bitcoin_high)
image_label1.grid(row=0, column=0, columnspan=2)
img_path = os.path.dirname(__file__) + "\\bitcoin_medium.png"
low_image1 = tkinter.PhotoImage(file=img_path)
resized_bitcoin_medium = low_image1.subsample(3,3)
image_label1 = tkinter.Label(root, image=resized_bitcoin_medium)
image_label1.grid(row=0, column=0, columnspan=2)
img_path = os.path.dirname(__file__) + "\\bitcoin_above_average.png"
low_image1 = tkinter.PhotoImage(file=img_path)
resized_bitcoin_above_average = low_image1.subsample(3,3)
image_label1 = tkinter.Label(root, image=resized_bitcoin_above_average)
image_label1.grid(row=0, column=0, columnspan=2)
img_path = os.path.dirname(__file__) + "\\bitcoin_moon.png"
low_image1 = tkinter.PhotoImage(file=img_path)
resized_bitcoin_moon = low_image1.subsample(3,3)
image_label1 = tkinter.Label(root, image=resized_bitcoin_moon)
image_label1.grid(row=0, column=0, columnspan=2)
#add label for pet condition
bitcoin_condition= tkinter.Label(text="Im low, feeling too lowww")
bitcoin_condition= tkinter.Label(text="Im high, feeling good and high")
bitcoin_condition.grid(row=1, column=0,columnspan=2)
raise_feed = tkinter. Button(root, text= "buy me !", command=raise_btn_click)
raise_feed.grid(row=2,column=0,)
#add price bar
price_fully_high_meter = ttk.Progressbar(root, orient= tkinter.HORIZONTAL, length=100, max=40000, mode = "determinate")
price_fully_high_meter["value"] = 0
price_fully_high_meter.grid(row=3,column=0,)
#progress bar text
price_fully_high_text = tkinter.Label(text="price : " + str(price))
price_fully_high_text.grid(row=4,column=0,)
raise1_feed = tkinter. Button(root, text= "post me !", command=pop_btn_click)
raise1_feed.grid(row=2,column=1,)
#add price bar
popularity_fully_high_meter = ttk.Progressbar(root, orient= tkinter.HORIZONTAL, length=100, max=100, mode = "determinate")
popularity_fully_high_meter["value"] = 0
popularity_fully_high_meter.grid(row=3,column=1,)
#progress bar text
popularity_fully_high_text = tkinter.Label(text="popularity: " + str(popularity))
popularity_fully_high_text.grid(row=4,column=1,)
mastertimer()
root.mainloop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Shawn Ramirez |