这是我编写的代码,其中包含带有选项的单选按钮和其他小部件,可以在 gui 面上查看……但它没有运行。我的意思是当有单选按钮代码时,它不会显示在 gui 面上。
from Tkinter import *
import tkFont
class Application:
def __init__(self, top):
self.top=top
# initializer for gui
self.gui()
def gui(self):
var="Application"
helv16=tkFont.Font(self.top,family="Helvetica",size=16,slant="italic")
label1=Label(self.top,text=var,font=helv16,bd=5,fg='#2ddef9',bg='#aaecaa',relief=RIDGE,padx=17,pady=4,justify="center")
label1.pack(side=TOP)
label2=Label(self.top,text='Proxy City', bd=3,fg='red',bg='#FFFFFF',relief=GROOVE,padx=18,pady=4,justify="center")
label2.grid(row=2, column=1)
# setting default proxy city to Bangalore
v=IntVar()
# initiallize to Bangalore
v.set(1)
list_city=[("Bangalore", 1), ("Chennai", 2), ("USA", 3), ("Chaina", 4)]
for city, num in list_city:
#print city, num
radiobttn=Radiobutton(self.top, text=city, padx=10,pady=5, variable=v, value=num,command=sel, bd=3,fg='red',bg='#FFFFFF',relief=GROOVE)
radiobttn.pack(anchor='w')
label3=Label(self.top,text='Add URL', bd=3,fg='red',bg='#FFFFFF',relief=GROOVE,padx=17,pady=4,justify="center")
entry3=Entry(self.top,relief=SUNKEN,justify=CENTER,bd=3,fg='#0000FF',font='arial')
label3.grid(row=3, column=0)
entry3.grid(row=3, column=1)
label3=Label(self.top,text='Add URL', bd=3,fg='red',bg='#FFFFFF',relief=GROOVE,padx=17,pady=4,justify="center")
entry3=Entry(self.top,relief=SUNKEN,justify=CENTER,bd=3,fg='#0000FF',font='arial')
label3.grid(row=4, column=0)
entry3.grid(row=4, column=1)
def sel():
selection = "You selected the option " + str(v.get())
label.config(text = selection)
def main():
top=Tk()
top.geometry("680x600+400+240")
top.title("Application")
Application(top)
top.mainloop()
if __name__=='__main__':
main()