-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtk-test.py
65 lines (52 loc) · 2.11 KB
/
tk-test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_widgets()
# Рекомендуемая практика определять все состовлющие класса в __init__.
# В данном случае имя hi_there и quit которое поторое используется для объекта
# tkinter.Button
self.hi_there: tk.Button = None
self.quit: tk.Button = None
def create_widgets(self):
self.hi_there = tk.Button(self, bg="green")
self.hi_there["text"] = "Hello World\n(click me)"
self.hi_there["command"] = self.say_hi
self.hi_there.pack(side="top")
self.quit = tk.Button(self, text="QUIT", fg="red", command=self.master.destroy)
self.quit.pack(side="bottom")
@staticmethod
def say_hi(self):
print("hi there, everyone!")
class App(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
self.entrythingy = tk.Entry()
self.entrythingy.pack()
# here is the application variable
self.contents = tk.StringVar()
# set it to some value
self.contents.set("this is a variable")
# Привязываем свойство "textvariable" объекта tkinter.Entry
# к переменной типа: tkinter.StringVar
self.entrythingy["textvariable"] = self.contents
# and here we get a callback when the user hits return.
# we will have the program print out the value of the
# application variable when the user hits return
self.entrythingy.bind('<Key-Return>', self.print_contents)
def print_contents(self, event):
print("hi. contents of entry is now ---->", self.contents.get())
print(type(self))
if self.contents.get() == "quit":
root.destroy()
def fun(event):
print(event)
root = tk.Tk()
app = App(master=root)
ent0 = tk.Entry()
ent0.pack()
ent0.bind('<Key>', fun)
app.mainloop()