How do I create a button in Python Tkinter to increase integer variable by 1 and display that variable?

天涯浪子 提交于 2019-12-23 04:33:07

问题


I am trying to create a Tkinter program that will store an int variable, and increase that int variable by 1 each time I click a button, and then display the variable so I can see that it starts out as 0, and then each time I click the button it goes up by 1. I am using python 3.4.

import sys
import math
from tkinter import *

root = Tk()
root.geometry("200x200")
root.title("My Button Increaser")

counter = 0
def nClick():
    counter + 1

def main_click():
    mLabel = Label(root, text = nClick).pack()


mButton1 = Button(text = "Increase", command = main_click, fg = "dark green", bg = "white").pack()

root.mainloop()

回答1:


You could use Tkinter variables. They are specially useful when you need to modify a data that other widgets might interact with. Here is a look alike code to the one in the question, but instead of defining counter as a normal variable, it is a variable from Tkinter.

import tkinter
import sys

root = tkinter.Tk()
root.geometry("200x200")
root.title("His Button Increaser")

counter = tkinter.IntVar()

def onClick(event=None):
    counter.set(counter.get() + 1)

tkinter.Label(root, textvariable=counter).pack()
tkinter.Button(root, text="Increase", command=onClick, fg="dark green", bg = "white").pack()

root.mainloop()

Instead of passing the value this variable holds to the text attribute of the Label, we assign the variable to textvariable attribute, so when the value of the variable gets updated, Label would update the displayed text accordingly.

When you want to change the value of the variable, you'd need to call the set() method of the variable object (see onClick) instead of assigning the value directly to it.




回答2:


Ok so there are a few things wrong with your code so far. My answer basically changes what you have already into the easiest way for it to do what you want.

Firstly you import libraries that you don't need/use (you may need them in your whole code, but for this question include a minimal example only). Next you must deifne the counter variable as a global variable, so that it will be the same in the function (do this inside the function as well). Also you must change counter + 1 to counter += 1 so it increments the variable

Now the code will be able to count, but you have set variables as Buttons, but then made them None type objects, to change this .pack() the variable on the next line. You can get rid of the second function as you only need one, then you change the command of the button and its text to counter. Now to update the text in the button, you use .config(text = counter) which configures the button.

Here is the final solution (changes button value and has no label, but this is easily changed):

from tkinter import *

root = Tk()
root.geometry("200x200")
root.title("My Button Increaser")

global counter
counter = 0

def nClick():
    global counter
    counter += 1
    mButton1.config(text = counter)

mButton1 = Button(text = counter, command = nClick, fg = "darkgreen", bg = "white")
mButton1.pack()

root.mainloop()

hope that helps!



来源:https://stackoverflow.com/questions/26598010/how-do-i-create-a-button-in-python-tkinter-to-increase-integer-variable-by-1-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!