简体   繁体   English

Tkinter:在另一个文件中使用条目值

[英]Tkinter: Using entry value in another file

When file1.py runs and the submit button is hit, the submit method will return entry.get() value, which is fine. 运行file1.py并单击“提交”按钮时,submit方法将返回entry.get()值,这很好。 However, if I wanted too transfer the entry.get() too another python file when the submit button is hit. 但是,如果我也想在单击“提交”按钮时将entry.get()也转移到另一个python文件中。 Is this possible? 这可能吗?

file1.py file1.py

from tkinter import *

root = Tk()

def submit():
    return entry.get()

entry = Entry(root)
entry.pack()
submit_button = Button(root, text = "Submit", command = submit)
submit_button.pack()

root.mainloop()

file2.py file2.py

import file1 as f1
from tkinter import messagebox
var = submit()
if var == "test":
   messagebox.showinfo("Hello")

This is what I'm wanting to do but having no luck with it atm. 这是我想要做的,但是atm没有运气。

You can pass the value to a function that you have defined in the other file like this. 您可以像这样将值传递给在其他文件中定义的函数。

p1.py p1.py

from tkinter import *
from p2 import new_print

root = Tk()

def submit():
    new_print(entry.get())

entry = Entry(root)
entry.pack()
submit_button = Button(root, text = "Submit", command = submit)
submit_button.pack()

root.mainloop()

p2.py p2.py

value_from_p1 = None

def new_print(value):
    global value_from_p1
    value_from_p1 = value
    print(value)

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

相关问题 从 Tkinter Entry 字段获取值到另一个 python 文件 - Get value from Tkinter Entry field to another python file 使用带有参数/条目的 tkinter 按钮打开另一个 python 文件 - Open another python file using tkinter button with parameters/entry 在 tkinter 中自动获取三个条目的总和作为另一个条目的值 - getting the sum of three entries to be the value of another entry automatically in tkinter 访问另一个 tkinter class 的输入字段值 - Accessing entry field value of one tkinter class in another Tkinter 将输入框值存储为其他文件的值 - Tkinter store entry box value as value for other file 无法从 Tkinter 输入框返回值 - 需要将输入值从一个脚本传递到另一个 - Unable to return a value from Tkinter Entry box - need to pass an entry value from one script to another 在Tkinter的另一个函数中使用条目/按钮中的变量 - Using the variable from entry/button in another function in Tkinter tkinter 回调事件从 Entry 中检索值并写入文本文件 - tkinter callback event to retrieve value from Entry and write to text file 获取tkinter Entry字段的值以在函数中(本地)使用 - Obtaining value of tkinter Entry field for using (locally) in a function 如何使用 tkinter 中的按钮设置“Entry”小部件的文本/值/内容 - How to set the text/value/content of an `Entry` widget using a button in tkinter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM