Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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 3.x Tkinter单选按钮只返回类中的原始set()值_Python 3.x_Tkinter - Fatal编程技术网

Python 3.x Tkinter单选按钮只返回类中的原始set()值

Python 3.x Tkinter单选按钮只返回类中的原始set()值,python-3.x,tkinter,Python 3.x,Tkinter,我试图让这个类工作,以便它将每个选项卡中的变量写入一个文件。当我尝试此操作时,我的RadioButton只返回值“0”,这是我最初设置的值,而不是希望更新的值。 同时,它在每个功能上运行,而不仅仅是当时页面上的功能。用openfile进行实验,当我试图清除文件时,似乎暗示单击一个更新按钮在一个窗口上调用更新函数,然后在下一个窗口上分别调用更新函数。我想我在范围方面有问题,但我不确定,也找不到,希望能得到帮助 这是目前的代码 import tkinter as tk from tkinter

我试图让这个类工作,以便它将每个选项卡中的变量写入一个文件。当我尝试此操作时,我的RadioButton只返回值“0”,这是我最初设置的值,而不是希望更新的值。 同时,它在每个功能上运行,而不仅仅是当时页面上的功能。用openfile进行实验,当我试图清除文件时,似乎暗示单击一个更新按钮在一个窗口上调用更新函数,然后在下一个窗口上分别调用更新函数。我想我在范围方面有问题,但我不确定,也找不到,希望能得到帮助

这是目前的代码

import tkinter as tk  
from tkinter import ttk

class orca_questions():
    def __init__(self):
        #this builds the window and notebool
        self.window = tk.Tk()
        self.window.title("Orca")
        self.tabs()


    def update(self, file = 'Orca_features', name = "", val = None):

        # this code should delete old instances
        #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        #        
        f = open(file, 'a')
        f.write(name + "_-_" + str(val)+'\n')
        f.close()    



    def radio_orca_tab(self, parent, options = [],
                       discription_text = '', feature_name = ""):

        feature_val = tk.StringVar()
        feature_val.set("0")


        #row and value for first button
        r = 1

        for o in options:
            #makes the radiobutton
            radiobutton1 = ttk.Radiobutton(parent, text=o,
                                       variable=feature_val, value=o)
            radiobutton1.grid(row=r, column=2, sticky=tk.W)

            #increases value and row, for differentiation
            r +=1


        ttk.Button(parent, text = "Update",
                   command = self.update(name = feature_name, val = feature_val.get())
                   ).grid(row=r-1 , column=4)

        ttk.Label(parent, text= discription_text).grid(row=3 , column=5)




    def tabs(self):
        #notebook_label = tk.Label(self.window, text="Notebook")
        #notebook_label.grid(row=3, column=2, sticky=tk.W, pady=3)

        tab = ttk.Notebook(self.window)
        tab.grid(row=1, column=1, sticky=tk.E + tk.W + tk.N + tk.S, padx=30,
                 pady=4)


        tab1 = tk.Frame(tab)
        tab.add(tab1, text="Gender", compound=tk.TOP)

        self.radio_orca_tab(parent = tab1, options = ['Male', 'Female'],
                       discription_text = 'Orca Whale gender can be dermined from fin shape. \nMale orcas have larger straighter fins, while females\nhave more dolphin like fins.', feature_name = "Gender")

        tab2 = tk.Frame(tab)        
        tab.add(tab2, text="Fin Condition", compound=tk.TOP)
        fin_desc = '''\tFins are considered missing the tip if less than the top 3rd 
        is broken off, if more the fin is considered broken'''

        self.radio_orca_tab(parent = tab2, options = ['Whole', 'Missing Tip', 'Broken', 'Floppy'],
                       discription_text = fin_desc, feature_name = "Fin Condition")


        tab3 = tk.Frame(tab)
        tab.add(tab3, text="Black Scars", compound=tk.TOP)
        scar_desc = '''\tIn the case that there are two different types of scars,
        description hierarchy goes: single lines<parallel lines<grid scars<mixed scars.
        A whale with both mixed scars and parallel lines is described as one having mixed scars.
        Grid scars are where two or more parallel scars cross over other parallel scars perpendicularly'''
        self.radio_orca_tab(parent = tab3, options = ['No Black Scars', 'Isolated Scars', 'Parallel Line Scars', 'Grid Scars', 'Mixed Scars', 'Uncertain'],
                       discription_text = scar_desc, feature_name = "Black Scars")


        tab4 = tk.Frame(tab)
        tab.add(tab4, text="White Scars", compound=tk.TOP)
        self.radio_orca_tab(parent = tab4, options = ['No White Scars', 'Isolated Scars', 'Parallel Line Scars', 'Grid Scars', 'Mixed Scars', 'Uncertain'],
                       discription_text = scar_desc, feature_name = "White Scars")


        tab5 = tk.Frame(tab)
        tab.add(tab5, text="Saddle", compound=tk.TOP)
        saddle_desc = '''\tAn open saddle has has protusions inside the saddle, 
        where as the closed saddle is more like a circle superimposed on the
        orca'''
        self.radio_orca_tab(parent = tab5, options = ['Opened', 'Closed', 'Not Sure'],
                       discription_text = saddle_desc, feature_name = "Saddle")


program = orca_questions()
program.window.mainloop()
感谢您的帮助,谢谢!我还是个新手,还不知道问题出在哪里

ttk.Button(parent, text = "Update",
           command = self.update(name = feature_name, val = feature_val.get())
           ).grid(row=r-1 , column=4)
命令参数接受函数引用作为输入,但您给它一个函数的返回值。因此,在创建按钮时,将调用更新函数并写入文件。此时,特征值当然仍然是0

要仍然能够为函数提供参数,请使用匿名函数:

ttk.Button(parent, text = "Update",
           command = lambda: self.update(name = feature_name, val = feature_val.get())
           ).grid(row=r-1 , column=4)

非常感谢。这很有帮助。如果你不介意我问的话,lambda有什么不同,它被当作函数而不是返回值?