[英]How can I return a value from an function that is executed from tkinter button?
Just started learning Python.刚开始学习Python。 I have created a file containing a class with tkinter fuctions.
我创建了一个包含 class 和 tkinter 功能的文件。
For Example:例如:
file1.py:文件 1.py:
import tkinter as tk
import pandas as pd
import os
class flt:
def func(var):
var['file'] = pd.read_excel(os.getcwd()+'\\xyz.xlsx')
if var[ch].get()==1:
var['file'] = "Filter this file accordingly" # Example
if var[hc].get()==1:
var['file'] = "filter this file accordingly" #Example
return var['file'] #I want this value in file2.py
def do_something(var):
root = var['root']
mainframe = var['mainframe']
var[ch] = IntVar()
var[hc] = IntVar()
ch1 = Checkbutton(mainframe, text='Filter by ABC',variable=var[ch])
ch2 = Checkbutton(mainframe, text='Filter by 123', variable=var[ch])
ch1.place(relx=0.4, rely=0.2)
ch2.place(relx=0.4, rely=0.3)
bt = tk.Button(mainframe, text='submit', command=lambda: flt.func(var))
bt.place(relx=0.5, rely=0.5)
And the another file, say file2.py:另一个文件,比如file2.py:
import tkinter as tk
root = tk.Tk()
mainframe = tk.Frame(root, relief='raised')
mainframe.place(relx=0.1, rely=0.1, width='1100', height='380')
val = {}
val['root'] = root
val['mainframe'] = mainframe
def chk:
import file1
value = file1.do_something(val)
print(value)
btn = tk.Button(mainframe, text='Check', command = chk)
btn.place(relx=0.5, rely=0.8)
root.mainloop()
I want the value "var['file']" (from func(var)>file1.py) in file2.py Every Answer is appreciated.我想要file2.py中的值“var ['file']”(来自func(var)> file1.py)每个答案都值得赞赏。 Thank you.
谢谢你。
You keep making your code more and more complicated and I still don't understand what you want.你不断使你的代码越来越复杂,我仍然不明白你想要什么。
So, I made this minimal program to show that when its the case that a new button appears, and the user presses that button, that a value is calculated and used in a call to an arbitrary function from another module: file1.py因此,我制作了这个最小的程序来显示当出现新按钮并且用户按下该按钮的情况下,计算一个值并将其用于从另一个模块调用任意 function: file1.py
import tkinter as tk
def func(var):
var['file'] = 42
return var['file']
def do_something(var, f):
# root = var['root']
mainframe = var['mainframe']
bt = tk.Button(mainframe, text='submit', command=lambda: f(func(var)))
# When the user presses submit, the function f is called with the 'return' value
bt.place(relx=0.5, rely=0.5)
file2.py文件2.py
import tkinter as tk
root = tk.Tk()
mainframe = tk.Frame(root, relief='raised')
mainframe.place(relx=0.1, rely=0.1, width='1100', height='380')
val = {}
val['root'] = root
val['mainframe'] = mainframe
def f(value):
print(value) # Just prints the value, but could do anything!
def chk():
import file1
file1.do_something(val, f) # f passed from here as a callback
# Note - no return value
btn = tk.Button(mainframe, text='Check', command = chk)
btn.place(relx=0.5, rely=0.8)
root.mainloop()
Note: the frame starts small and you need to drag it to resize it bigger to see the buttons.注意:框架从小开始,您需要拖动它以将其调整为更大以查看按钮。
Here first you have to create the respective class and then import the file.首先,您必须创建相应的 class 然后导入文件。
File1:文件1:
#creating a class
class person:
def __init__ f1(self,x):# this function will be called everytime when the class is called
self.value=x
File2:文件2:
import file1 #importing the file (file name)
x=file1.person(10)#calling the class from file1 (parameter given as 10)
print(x.value) #printing the value.
In your case:在你的情况下:
file 1:文件 1:
import tkinter as tk
class flt:
def __init__(self,x):
self.value = x*x
def do_something(self,var):
#some stuff
file 2:文件 2:
import file1
import tkinter as tk
#basic tkinter window code
trial=file1.flt('value')
test=trial.value #here you store the value of file1
def chk:
value = file1.do_something(val)
print(value)
btn = tk.Button(mainframe, text='Check', command = chk)
btn.place(relx=0.5, rely=0.8)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.