简体   繁体   English

用户选择参数值的 Tkinter 小部件,但如何存储这些值?

[英]Tkinter widget where User chooses parameter values, but how do I store these values?

My objective is to create three variables called 'Number of pipes','Anisotropy ratio', 'Filter depth (in cm)' which values are chosen by the user (through a User interface).我的目标是创建三个变量,称为“管道数”、“各向异性比”、“过滤器深度(以厘米为单位)”,这些值由用户(通过用户界面)选择。 I achieved to create the scrollbar of Tkinter and to enter values but these are not stored as variables.我实现了创建 Tkinter 的滚动条并输入值,但这些值不存储为变量。 Could someone give me a hand solving that?有人可以帮我解决这个问题吗? I am new in Python and specially I am struggling with Tkinter.我是 Python 新手,特别是我在用 Tkinter 苦苦挣扎。 The code is developped in Python 3.7:代码是用 Python 3.7 开发的:

root = tk.Tk()

root.geometry('1000x1000') 



#Description show in window
info=tk.Label(root,anchor='e')
info.pack()

#Parameters


parameters = ('Number of pipes','Anisotropy ratio', 'Filter depth (in cm)')

def ask_parameter(entry):

    user_pipes = str (entry['Number of pipes'].get()) 


    user_aniso = str (entry['Anisotropy ratio'].get()) #effective screen length = b

    user_depth = str (entry['Filter depth (in cm)'].get()) 
    print(user_pipes,user_aniso,user_depth)


#if parameters 
#        return True
#    else:
#        tkinter.messagebox.showwarning ('Only numbers', 'Try again')
#        return True
#        


def form(parameters):
    entry = {}
    for parameter in parameters:
        print(parameter)
        row = tk.Frame(root)
        lab = tk.Label(row, width=15, text=parameter+": ", anchor='w')
        ent = tk.Entry(row)      
        row.pack(side=tk.TOP, 
                 fill=tk.X, 
                 padx=2, 
                 pady=2)
        lab.pack(side=tk.LEFT)
        ent.pack(side=tk.RIGHT, expand=tk.YES,fill=tk.X)
        entry[parameter] = ent
    return entry


if __name__ == '__main__':
    ents = form(parameters)


save_button = tk.Button(root)
save_button.configure(text='Save', command=lambda: ask_parameter(ents))
save_button.pack()
root.mainloop()

Any problem is shown with the code but the parameters are not stored as variables with the entered values.代码会显示任何问题,但参数不会存储为具有输入值的变量。 Thank you very much for your time.非常感谢您的宝贵时间。

您的代码将通过 GUI 输入的值存储到变量中,您的问题可能是您的变量不是全局变量,因此您将无法在创建它们的函数之外使用它们。您可以解决这个问题带有return ,或者通过将global varname放在每个变量的定义之上以及每个将使用它的函数的开头来使它们成为全局变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在tkinter中存储来自Entry小部件的值以进行循环? - How to store values from an Entry widget for loop in tkinter? 如何在Python Tkinter GUI的“标签”小部件中显示更新的值? - How do I show updated values in the 'label' widget in Python Tkinter GUI? 如何对tkinter Scale小部件的某些值执行操作? - How to do an action for certain values of tkinter Scale widget? 如何根据Django中的用户输入将值存储在模型中? - How do I store values in a model based on user input in django? 如何存储在 for 循环中生成的多个值以传递给 tkinter 按钮循环中的函数? - How do I store multiple values generated in a for loop to pass to a funtion within a tkinter button loop? 在tkinter中获取小部件值 - Obtaining widget values in tkinter 如何将参数值列表添加到一组数据框,其中该参数对于每个数据框都有不同的值? - How do I add a list of values for a parameter to a group of dataframes, where that parameter has a different value for each dataframe? 如何从 Tkinter GUI 表单存储用户数据? - How do I store user data from a Tkinter GUI form? Python:如何在 Tkinter 上存储用户输入的文本? - Python: How do i store a text input from the user on Tkinter? 如何在 AWS Lambda 中缓存多个 AWS Parameter Store 值? - How do I cache multiple AWS Parameter Store values in an AWS Lambda?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM