python - 如何在 Tkinter 中获取单选按钮输出?

标签 python tkinter

这是我编写的代码,用于在 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()

最佳答案

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

您的固定代码如下所示:

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

关于python - 如何在 Tkinter 中获取单选按钮输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19953838/

相关文章:

python - 在滚动窗口内放置绘图区域时防止滚动条出现

python - 在 Python3 ubuntu 上安装 tkinter

python - Tkinter 中的文本输入

python - 阻止 Sprite 离开 tkinter 窗口

python - 如何创建一个 tkinter 切换按钮?

python - 自动编码器内 conv2d 层的形状大小不匹配

python - 为什么我会收到此错误

python - 如何只将部分字符串值放在列中?

python - 为什么在 Docker 镜像中使用 requirements.txt

python - python中的定期消息框