forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWidgetsDemo.py
61 lines (52 loc) · 2.36 KB
/
WidgetsDemo.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
from tkinter import * # Import tkinter
class WidgetsDemo:
def __init__(self):
window = Tk() # Create a window
window.title("Widgets Demo") # Set a title
# Add a button, a check button, and a radio button to frame1
frame1 = Frame(window) # Create and add a frame to window
frame1.pack()
self.v1 = IntVar()
cbtBold = Checkbutton(frame1, text = "Bold",
variable = self.v1, command = self.processCheckbutton)
self.v2 = IntVar()
rbRed = Radiobutton(frame1, text = "Red", bg = "red",
variable = self.v2, value = 1,
command = self.processRadiobutton)
rbYellow = Radiobutton(frame1, text = "Yellow",
bg = "yellow", variable = self.v2, value = 2,
command = self.processRadiobutton)
cbtBold.grid(row = 1, column = 1)
rbRed.grid(row = 1, column = 2)
rbYellow.grid(row = 1, column = 3)
# Add a button, a check button, and a radio button to frame1
frame2 = Frame(window) # Create and add a frame to window
frame2.pack()
label = Label(frame2, text = "Enter your name: ")
self.name = StringVar()
entryName = Entry(frame2, textvariable = self.name)
btGetName = Button(frame2, text = "Get Name",
command = self.processButton)
message = Message(frame2, text = "It is a widgets demo")
label.grid(row = 1, column = 1)
entryName.grid(row = 1, column = 2)
btGetName.grid(row = 1, column = 3)
message.grid(row = 1, column = 4)
# Add a text
text = Text(window) # Create a text add to the window
text.pack()
text.insert(END,
"Tip\nThe best way to learn Tkinter is to read ")
text.insert(END,
"these carefully designed examples and use them ")
text.insert(END, "to create your applications.")
window.mainloop() # Create an event loop
def processCheckbutton(self):
print("check button is "
+ ("checked " if self.v1.get() == 1 else "unchecked"))
def processRadiobutton(self):
print(("Red" if self.v2.get() == 1 else "Yellow")
+ " is selected " )
def processButton(self):
print("Your name is " + self.name.get())
WidgetsDemo() # Create GUI