Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 自动选择单选按钮tkinter_Python_Tkinter - Fatal编程技术网

Python 自动选择单选按钮tkinter

Python 自动选择单选按钮tkinter,python,tkinter,Python,Tkinter,我一直在尝试使用*kwargs使用Radiobutton创建一个选项菜单。 不幸的是,发送的变量顺序并没有如预期的那样:简单、中等、硬、极端。而且,即使我将v设置为一个特定的值,所有选项都会同时被选中 我是不是遗漏了什么 #!/usr/bin/env python # -*- coding: utf-8 -*- import Tkinter as tk from Tkinter import * class MainApplication(tk.Frame): def __init

我一直在尝试使用
*kwargs
使用Radiobutton创建一个选项菜单。 不幸的是,发送的变量顺序并没有如预期的那样:简单、中等、硬、极端。而且,即使我将v设置为一个特定的值,所有选项都会同时被选中

我是不是遗漏了什么

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import Tkinter as tk
from Tkinter import *


class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

    def onClick(self, event):
        print("Clicked")

    def qChoice(self, **kwargs):
        v = IntVar()
        v.set(1)
        for key, value in kwargs.iteritems():
            self.Rbutton = Radiobutton(text=key, variable=v, value=value)
            self.Rbutton.grid(sticky=W)

    def basics(self):
        self.label = Label(text="Enter name:")
        self.label.grid(column=0, row=0, sticky="E")
        self.entry = Entry()
        self.entry.grid(column=1, row=0)
        self.button = Button(text="Enter")
        self.button.bind("<Button-1>", self.onClick)
        self.button.grid(column=3, row=0)
        self.qChoice(Easy=1,Medium=2,Hard=3,Extreme=4)

if __name__ == "__main__":
    root = tk.Tk()
    App = MainApplication(root)
    App.basics()
    root.mainloop()
#/usr/bin/env python
#-*-编码:utf-8-*-
将Tkinter作为tk导入
从Tkinter进口*
类main应用程序(tk.Frame):
定义初始化(自、父、*args、**kwargs):
tk.Frame.\uuuuu init\uuuuuu(self,parent,*args,**kwargs)
self.parent=parent
def onClick(自我,事件):
打印(“单击”)
def qChoice(自我,**kwargs):
v=IntVar()
v、 第(1)组
对于键,使用kwargs.iteritems()表示的值:
self.Rbutton=Radiobutton(text=key,variable=v,value=value)
self.Rbutton.grid(粘性=W)
def基础知识(自我):
self.label=标签(text=“输入名称:”)
self.label.grid(列=0,行=0,sticky=“E”)
self.entry=entry()
self.entry.grid(列=1,行=0)
self.button=按钮(text=“Enter”)
self.button.bind(“,self.onClick)
self.button.grid(列=3,行=0)
self.qChoice(简单=1,中等=2,硬=3,极端=4)
如果名称=“\uuuuu main\uuuuuuuu”:
root=tk.tk()
App=main应用程序(根)
App.basics()
root.mainloop()

您正在为
v
使用一个局部变量,该变量在函数退出时被垃圾回收。您需要保留一份永久参考:

def qChoice(self, **kwargs):
    self.v = Intvar()
    ...

另一方面,您不需要两个导入语句。使用其中一个,但不能同时使用两个。理想情况下,使用第一个:

import Tkinter as tk
您的
IntVar()

def qChoice(self, **kwargs):
    # changed to self.v from v
    self.v = IntVar()
    # .set(None) leaves all the self.v instances blank
    # if you did .set('1'), the first one will be auto-selected
    # you can also remove this line if you want them to start blank
    self.v.set(None)
    for key, value in kwargs.iteritems():
        self.Rbutton = Radiobutton(text=key, variable=self.v, value=value)
        self.Rbutton.grid(sticky=W)
类似的主题:


非常感谢你,我甚至都没有这样想。你知道为什么*Kwarg没有正确迭代吗?