简体   繁体   English

在Tkinter的另一个函数中使用条目/按钮中的变量

[英]Using the variable from entry/button in another function in Tkinter

When I press the button, I want it to get the Entry and -for future things- use it in another function. 当我按下按钮时,我希望它获取Entry并(为了将来使用)在另一个功能中使用它。

import tkinter

def ButtonAction():
    MyEntry = ent.get() #this is the variable I wanna use in another function

den = tkinter.Tk()
den.title("Widget Example")

lbl = tkinter.Label(den, text="Write Something")
ent = tkinter.Entry(den)
btn = tkinter.Button(den, text="Get That Something", command = ButtonAction )

lbl.pack()
ent.pack()
btn.pack()

den.mainloop()

print MyEntry #something like this maybe. That's for just example

I will use this thing as a search tool. 我将把这个东西用作搜索工具。 Entry window will appear, get that "entry" from there and search it in files like: 输入窗口将出现,从那里获取“输入”,然后在文件中进行搜索,例如:

if MyEntry in files:
 #do smth

I know I can handle the problem with using globals but from what I've read it's not recommended as a first solution. 我知道我可以使用全局变量来解决该问题,但是根据我的阅读,不建议将其作为第一个解决方案。

Structure the program using class. 使用类构造程序。

import tkinter

class Prompt:
    def button_action(self):
        self.my_entry = self.ent.get() #this is the variable I wanna use in another function

    def __init__(self, den):
        self.lbl = tkinter.Label(den, text="Write Something")
        self.ent = tkinter.Entry(den)
        self.btn = tkinter.Button(den, text="Get That Something", command=self.button_action)
        self.lbl.pack()
        self.ent.pack()
        self.btn.pack()

den = tkinter.Tk()
den.title("Widget Example")
prompt = Prompt(den)
den.mainloop()

You can access the input using prompt.my_entry later. 您可以稍后使用prompt.my_entry访问输入。

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

相关问题 如何通过按下按钮从 tkinter 输入字段返回变量以在另一个 function 中使用? - How do I return a variable from a tkinter Entry field to use in another function by pressing a button? tkinter函数的入口变量 - tkinter entry variable from function 如何从 tkinter 输入框获取输入到另一个 function 中的变量? - How to get input from a tkinter entry box, to a variable in another function? Tkinter-在类中使用Button命令从另一个类中调用函数 - Tkinter - Using a Button command in a class to call a function from another class python,tkinter 条目到达 function(错误来自 F 按钮的 ZC1C425268E68385D14AB5074C17ZA49) - python, tkinter entry get to function (error was with function call from button) 使用带有参数/条目的 tkinter 按钮打开另一个 python 文件 - Open another python file using tkinter button with parameters/entry Tkinter:使用按钮从条目中获取值并存储在变量中 - Tkinter: get value from entry with button and store in variable 如何使用来自 tk.Entry 的变量将 tkinter 按钮与 function 一起使用? - How do I use a tkinter button with a function, using variables from tk.Entry? 使用按钮输入 tkinter 条目 - Typing in a tkinter entry using a button 如何使用函数将变量分配给tkinter的输入框 - How to assign a variable to tkinter's entry box using a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM