We will have you re-appear in the Program made by Mr. K, a junior high school student. The execution result looks like this When you press the button, the value will count up. The program that used tk for the first time is well done. I think it's good to be able to realize a program that works as expected.
Made by Mr. K
import tkinter as tk
count = 0
def dispLabel():
global count
count = count + 1
lbl.configure(text = str(count) + "Pressed twice")
root = tk.Tk()
root.geometry("300x80")
lbl = tk.Label(text="Count button")
btn = tk.Button(text="Press", command = dispLabel)
lbl.pack()
btn.pack()
tk.mainloop()
However, the program that stores the counter value as a global variable and is like a model for beginners.
In an actual program, the processing content may be more complicated, or similar processing may occur many times. By introducing object-oriented programming, you can create programs efficiently. Mr. K's program only had a button that counts up the numbers, but assuming that other buttons will be added to this, I will try to modify it into an object-oriented program
With K's programming method, the problem with adding buttons is that it is necessary to add global variables such as count1 and count2 to the button addition. The process of displaying in TK and the process of counting up are integrated and not separated. It seems good to create an object to count and have a count variable inside this object.
class Counter():
def __init__(self):
self.count = 0
def get_next_value(self):
self.count +=1
return str(self.count)
Initialize count with \ _ \ _ init__ () and count up every time you call get_next_value ().
I also made DownCounter and CounterAlphabet. The Application class is an object that displays buttons and labels and updates the display when the button is pressed. I tried to manage the Application class by referring to Explanation of Tcl / Tkcl / Tk.
import tkinter as tk
class Counter():
def __init__(self):
self.count = 0
def get_next_value(self):
self.count +=1
return str(self.count)
class DownCounter(Counter):
def __init__(self):
self.count = 99
def get_next_value(self):
self.count -=1
return str(self.count)
class CounterAlphabet(Counter):
word_list = ["zero", "one", "two", "three"]
def get_next_value(self):
self.count +=1
try:
return self.word_list[self.count]
except IndexError:
return "many"
class Application(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.master = master
self.master.geometry("300x160")
self.pack()
def add_button(self, counter, label="Count button", msg="Pressed twice"):
lbl = tk.Label(text=label)
def update_label():
str = counter.get_next_value()
lbl.configure(text = str + msg )
btn = tk.Button(text="Press", command = update_label)
lbl.pack()
btn.pack()
root = tk.Tk()
app = Application(root)
counter1 = Counter()
counter2 = DownCounter()
counter3 = CounterAlphabet()
app.add_button(counter1, "Count button")
app.add_button(counter2, "Countdown button","Times")
app.add_button(counter3, "Count button(English)"," times")
app.mainloop()
Execution result
Thank you, Mr. K, for providing the program.
Mr. shiracamus commented on a more sophisticated example. Please refer to the comment section. Further magical modifications are welcome.
Recommended Posts