Just started learning Python. I have created a file containing a class with tkinter fuctions.
For Example:
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)
And the another file, say 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. 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
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
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.
File1:
#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:
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:
import tkinter as tk
class flt:
def __init__(self,x):
self.value = x*x
def do_something(self,var):
#some stuff
file 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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.