如何在Tkinter中获得单选按钮输出?

时间:2013-11-13 12:29:03

标签: python tkinter

这是我编写的代码,其中有单选按钮和其他小部件可以在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()

1 个答案:

答案 0 :(得分:1)

在很多情况下,您没有在类范围中使用变量,但仅在方法的范围内,但我修复了它们

您的固定代码如下:

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")
        self.label1=Label(self.top,text=var,font=helv16,bd=5,fg='#2ddef9',bg='#aaecaa',relief=RIDGE,padx=17,pady=4,justify="center")
        self.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").pack()
        # setting default proxy city to Bangalore
        self.v=IntVar()
        # initiallize to Bangalore
        self.v.set(1)


        list_city=[("Bangalore", 1), ("Chennai", 2), ("USA", 3), ("Chaina", 4)]
        for city, num in list_city:
            #print city, num
            Radiobutton(self.top, text=city, padx=10,pady=5, variable=self.v, value=num,command=self.sel, bd=3,fg='red',bg='#FFFFFF',relief=GROOVE).pack(anchor='w')

        label3=Label(self.top,text='Add URL', bd=3,fg='red',bg='#FFFFFF',relief=GROOVE,padx=17,pady=4,justify="center").pack()
        entry3=Entry(self.top,relief=SUNKEN,justify=CENTER,bd=3,fg='#0000FF',font='arial').pack()

    def sel(self):
        selection = "You selected the option " + str(self.v.get())
        self.label1.config(text = selection)


def main():
    top=Tk()
    top.geometry("680x600+400+240")
    top.title("Application")
    Application(top)
    top.mainloop()

if __name__=='__main__':
    main()

注意:我从未理解您覆盖现有标签的部分代码! 这是不好的做法,不应该遵循

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)

正如@BryanOakley所述,您可以在一个主循环中同时使用packgrid方法,但不能同时使用这两种方法来共享同一个父级的小部件。