Python 如何从tkinter按钮执行的函数返回值?

Python 如何从tkinter按钮执行的函数返回值?,python,tkinter,Python,Tkinter,刚开始学习Python。 我创建了一个文件,其中包含一个具有tkinter功能的类 例如: file1.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'

刚开始学习Python。 我创建了一个文件,其中包含一个具有tkinter功能的类

例如:

file1.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)
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()
#creating a class
class person:
    def __init__ f1(self,x):# this function will be called everytime when the class is called
        self.value=x
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.
import tkinter as tk
class flt:
    def __init__(self,x):
        self.value = x*x

    def do_something(self,var):
        #some stuff
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()
另一个文件是file2.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)
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()
#creating a class
class person:
    def __init__ f1(self,x):# this function will be called everytime when the class is called
        self.value=x
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.
import tkinter as tk
class flt:
    def __init__(self,x):
        self.value = x*x

    def do_something(self,var):
        #some stuff
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()
我想要file2.py中的值“var['file']”(来自func(var)>file1.py)
每一个答案都很感激。谢谢。

首先,您必须创建相应的类,然后导入文件

文件1:

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)
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()
#creating a class
class person:
    def __init__ f1(self,x):# this function will be called everytime when the class is called
        self.value=x
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.
import tkinter as tk
class flt:
    def __init__(self,x):
        self.value = x*x

    def do_something(self,var):
        #some stuff
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()
文件2:

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)
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()
#creating a class
class person:
    def __init__ f1(self,x):# this function will be called everytime when the class is called
        self.value=x
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.
import tkinter as tk
class flt:
    def __init__(self,x):
        self.value = x*x

    def do_something(self,var):
        #some stuff
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()

就你而言:

文件1:

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)
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()
#creating a class
class person:
    def __init__ f1(self,x):# this function will be called everytime when the class is called
        self.value=x
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.
import tkinter as tk
class flt:
    def __init__(self,x):
        self.value = x*x

    def do_something(self,var):
        #some stuff
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()
文件2:

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)
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()
#creating a class
class person:
    def __init__ f1(self,x):# this function will be called everytime when the class is called
        self.value=x
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.
import tkinter as tk
class flt:
    def __init__(self,x):
        self.value = x*x

    def do_something(self,var):
        #some stuff
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()

你让你的代码变得越来越复杂,我还是不明白你想要什么

因此,我制作了这个最小的程序,以显示当出现一个新按钮,用户按下该按钮时,会计算一个值,并在从另一个模块调用任意函数时使用该值: file1.py

将tkinter作为tk导入
def func(变量):
var['file']=42
返回var['file']
def do_某事(变量,f):
#root=var['root']
mainframe=var['mainframe']
bt=tk.Button(大型机,text='submit',command=lambda:f(func(var)))
#当用户按下submit时,函数f被调用为“return”值
bt.place(relx=0.5,rely=0.5)
file2.py

将tkinter作为tk导入
root=tk.tk()
mainframe=tk.Frame(根,relief='raised')
mainframe.place(relx=0.1,rely=0.1,width='1100',height='380')
val={}
val['root']=根
val['mainframe']=大型机
def f(值):
打印(值)#只打印值,但可以做任何事情!
def chk():
导入文件1
file1.do_something(val,f)#f作为回调从这里传递
#注意-无返回值
btn=tk.按钮(大型机,text='Check',command=chk)
基站位置(relx=0.5,rely=0.8)
root.mainloop()

注意:框架开始时很小,您需要拖动它以将其调整到更大的大小,才能看到按钮。

在您的
file2.py
中,像导入
一样从file2导入flt
。确保两个文件位于同一位置您的代码充满错误。您应该使用完整的错误回溯或更正的代码来更新您的问题。@Epsi95两个文件位于同一位置,是的,我确实使用了导入,如从文件1导入flt导入
,但上面的代码就是一个示例。谢谢你的建议。@quamrana原始代码有100行。在这段代码中,我同意可能有很多错误,但我的问题是,当从tkinter按钮调用时,有没有办法从文件中的另一个类获取值?谢谢您阅读您提供的代码行之间的内容,用户可以按
submit
按钮。所以,当他们这样做(执行
flt.func()
)时,您希望在
file1.py
中发生什么?是的,但对我来说更复杂。但这解决了我的问题,非常感谢夸姆拉纳