tkinter获得选择radiobutton

时间:2016-10-13 13:04:15

标签: python tkinter

如何将此应用程序的优先级附加到文件中,如下面的代码所示?

    #Option boxes for assigning a priority level to the request
    priority = StringVar()
    Radiobutton(self.f4, text = "High", variable=priority, value="1").grid(row = 4, column = 1, sticky = W)
    Radiobutton(self.f4, text = "Low", variable=priority, value="2").grid(row = 6, column = 1, sticky = W)


    #Button for "Confirm application"
    self.app_con = Button(self.f4, text=" Confirm and finish ", command=self.apphistory)
    self.app_con.grid(row=4, column=2


def apphistory(self):
    fo = open("Application_History.txt", "a")
    fo.writelines(["Priotiry: "+str(priority.get()),"\n"])
    fo.close

2 个答案:

答案 0 :(得分:0)

您是否尝试过更改

  

变量=优先

  

变量= self.priority

这样,Radiobutton知道在哪里搜索变量(在您的程序/类中),而不是在Radiobutton-Class本身中查找它。

正如评论中所提到的,您需要创建变量

  

self.priority

答案 1 :(得分:0)

你的范围显然是错误的。 尝试这样的事情:

 self.app_con = Button(self.f4, text=" Confirm and finish ", command=lambda:self.apphistory(priority))
...
def apphistory(self, priority):
    fo = open("Application_History.txt", "a")
    fo.writelines(["Priotiry: "+str(priority.get()),"\n"])
    fo.close()