3

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

1 回答 1

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方法,但不能同时将这两种方法用于共享同一父级的小部件。

于 2013-11-13T14:15:28.627 回答