Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- self.firstRadioButton = Radiobutton(self.__canvas, text="ONE", fg='white', bg=BACKGROUND_COLOR, variable=self.selectedONE, value=1)
- self.secondRadioButton = Radiobutton(self.__canvas, text="TWO", fg='white', bg=BACKGROUND_COLOR, variable=self.selectedTWO, value=2)
- import tkinter
- # function that is called when you select a certain radio button
- def selected():
- print(var.get())
- root = tkinter.Tk()
- var = tkinter.StringVar() #used to get the 'value' property of a tkinter.Radiobutton
- # Note that I added a command to each radio button and a different 'value'
- # When you press a radio button, its corresponding 'command' is called.
- # In this case, I am linking both radio buttons to the same command: 'selected'
- rb1 = tkinter.Radiobutton(text='Radio Button 1', variable=var, value="Radio 1", command=selected)
- rb1.pack()
- rb2 = tkinter.Radiobutton(text='Radio Button 2', variable=var, value="Radio 2", command=selected)
- rb2.pack()
- root.mainloop()
- self.secondRadioButton.bind('<ButtonRelease-1>', self.__function_name_that_should_run_when_radiobtn_selected)
- #!/usr/bin/env python
- from Tkinter import Tk, Frame, Label, Entry, Button, TOP, LEFT, RIGHT, X, YES, END,
- Radiobutton, StringVar,
- Canvas
- def padded(text):
- # return a padded 80-column string
- line = text
- line += (30 - len(text)) * ' '
- return line
- class PRB:
- def __init__(self, master):
- # self.entries = {}
- frame = Frame(master)
- frame.pack(side=TOP, fill=X, expand=YES, padx=5, pady=5)
- root.geometry("360x220")
- self.w = Canvas(master, width=560, height=400)
- self.w.pack()
- self.w.create_line(11, 5, 330, 5)
- self.w.create_line(11, 37, 330, 37)
- self.w.create_line(11, 73, 330, 73)
- self.w.create_line(11, 5, 11, 73)
- self.w.create_line(110, 5, 110, 73)
- self.w.create_line(330, 5, 330, 73)
- self.w.create_line(275, 5, 275, 73)
- self.ent1 = Entry(master)
- self.ent1.pack(side=RIGHT, expand=YES, fill=X)
- self.ent1.place(x=130, y=100, width=130)
- self.ent1.delete(0, END)
- self.ent1.insert(0, 'Enter a pass to pass!')
- self.ent1.focus_set()
- self.ent2 = Entry(master)
- self.ent2.pack(side=RIGHT, expand=YES, fill=X)
- self.ent2.place(x=130, y=140, width=130)
- self.ent2.delete(0, END)
- self.ent2.insert(0, 'Enter a pass to pass!')
- self.ent2.focus_set()
- self.v = StringVar()
- self.rb1 = Radiobutton(root, text="Type 1", variable=self.v, value=1)
- self.rb1.pack(side=TOP)
- self.rb1.place(x=20, y=20)
- self.rb2 = Radiobutton(root, text="Type 2", variable=self.v, value=2)
- self.rb2.pack(side=TOP)
- self.rb2.place(x=20, y=55)
- self.b1 = Button(master, width=10, text='Test_X', command=self.Test_X)
- self.b1.pack(side=RIGHT)
- self.b1.place(x=20, y=100)
- self.b2 = Button(master, width=10, text='Test_Y', command=self.Test_Y)
- self.b2.pack(side=RIGHT)
- self.b2.place(x=20, y=140)
- self.quit = Button(master, width=10, text='Cancel', bg='red', fg='white', command=root.quit)
- self.quit.pack(side=LEFT)
- self.quit.place(x=20, y=180)
- self.pasMSG = 'PASS'
- self.pasMSG2 = 'PASS'
- self.msg = padded('Please, select a type!')
- self.l1 = Label(root, text=self.msg, anchor='w')
- self.l1.pack()
- self.l1.place(x=120, y=20)
- self.l2 = Label(master, text=self.msg, anchor='w')
- self.l2.pack(side=RIGHT)
- self.l2.place(x=120, y=55)
- self.p1 = Label(master, text=self.pasMSG, anchor='w')
- self.p1.pack(side=RIGHT)
- self.p1.place(x=285, y=20)
- self.p2 = Label(master, text=self.pasMSG2, anchor='w')
- self.p2.pack(side=RIGHT)
- self.p2.place(x=285, y=55)
- def Test_X(self):
- if self.v.get() == '1':
- msg = padded('Type 1 Test_X')
- self.l1 = Label(root, text=msg, anchor='w')
- self.l1.pack()
- self.l1.place(x=120, y=20)
- if self.ent1.get().strip() == 'pass':
- pasMSG = padded('PASS!')
- co = 'green'
- else:
- pasMSG = padded('FAIL :(')
- co = 'red'
- self.p1 = Label(root, text=pasMSG, bg=co, fg='white', anchor='w')
- self.p1.pack(side=RIGHT)
- self.p1.place(x=285, y=20, width=40)
- # self.p1.config(height = )
- elif self.v.get() == '2':
- msg = padded('Type 2 Test_X')
- self.l1 = Label(root, text=msg, anchor='w')
- self.l1.pack()
- self.l1.place(x=120, y=20)
- if self.ent1.get().strip() == 'pass':
- pasMSG = padded('PASS!')
- co = 'green'
- else:
- pasMSG = padded('FAIL :(')
- co = 'red'
- self.p1 = Label(root, text=pasMSG, bg=co, fg='white', anchor='w')
- self.p1.pack(side=RIGHT)
- self.p1.place(x=285, y=20, width=40)
- def Test_Y(self):
- if self.v.get() == '1':
- msg2 = padded('Type 1 Test_Y')
- self.l2 = Label(root, text=msg2, anchor='w')
- self.l2.pack()
- self.l2.place(x=120, y=55)
- if self.ent2.get().strip() == 'pass':
- pasMSG2 = padded('PASS')
- co = 'green'
- else:
- pasMSG2 = padded('FAIL :(')
- co = 'red'
- self.p2 = Label(root, text=pasMSG2, bg=co, fg='white', anchor='w')
- self.p2.pack(side=RIGHT)
- self.p2.place(x=285, y=55, width=40)
- # self.p1.config(height = )
- elif self.v.get() == '2':
- msg2 = padded('Type 2 Test_Y')
- self.l2 = Label(root, text=msg2, anchor='w')
- self.l2.pack()
- self.l2.place(x=120, y=55)
- if self.ent2.get().strip() == 'pass':
- pasMSG2 = padded('PASS!')
- co = 'green'
- else:
- pasMSG2 = padded('FAIL :(')
- co = 'red'
- self.p2 = Label(root, text=pasMSG2, bg=co, fg='white', anchor='w')
- self.p2.pack(side=RIGHT)
- self.p2.place(x=285, y=55, width=40)
- if __name__ == '__main__':
- root = Tk()
- prb = PRB(root)
- root.title('put "pass" in the entries to pass the test!')
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement