Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with if in tkinter
#1
I need help with if. This code just when you click a button it +1 to a counter and the problem is I need something when the counter reaches 10 5 points substract and something will happned and I dont know how to do it I tried this but nothing
if counter == "5": // does not work    //something if counter == 5: // does not work    //something
from tkinter import * root = Tk() root.geometry("200x200") root.title("My Clicker Game") global counter counter = 0 def nClick():     global counter     counter += 1     print("hello")     mLabel.config(text = counter) mButton1 = Button(text = "Mine Gold", command = nClick, fg = "darkgreen", bg = "white") mButton1.pack() mButton2 = Button(text = "Goodbye", command = root.destroy, fg = "darkblue", bg = "white") mButton2.pack() mLabel = Label() mLabel.pack() root.mainloop()

solved it thanks people for listening
here is the right code
from tkinter import * abc = 1 root = Tk() root.geometry("200x200") root.title("My Clicker Game") global counter counter = 0 def nClick():    global counter    counter += abc    ab = counter    if ab == 5:        print("hello")    mLabel.config(text = counter) mButton1 = Button(text = "Mine Gold", command = nClick, fg = "darkgreen", bg = "white") mButton1.pack() mButton2 = Button(text = "Goodbye", command = root.destroy, fg = "darkblue", bg = "white") mButton2.pack() mLabel = Label() mLabel.pack() root.mainloop()
The problem was I didnt reference the updated counter
Reply
#2
Generally, with a GUI, you are going to want to do OOP programming with classes. At that point, it's better to have counter be an attribute of a class, and nClick be a method that updates the counter. Global variables should be used sparingly, and generally only for constants.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Your other post,don't start a new thread with same subject.
Reply


User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.