У меня есть два класса, и я хочу получить доступ к методам объектов в классах.Я могу получить доступ к приложению объекта всем методом из класса MainWindow
.Потому что он создает этот объект в __main__
.Но я делаю кнопку, которая открывает новый кадр в функции new_window и присваивает ей класс SecondWindow
.
Как создать объект для этого класса и вызвать методы этого класса по объекту? Проблема в том, что я не знаю, как общаться между классами объектами.
Может кто-нибудь показать, что я должен определить, чтобы иметь возможность вызова из методов класса MainWindow из класса SecondWindow?Я получаю эту ошибку
Ошибка типа: TestFunction () отсутствует 1 обязательный позиционный аргумент: 'self'
Код:
from tkinter import *
class MainWindow():
def __init__(self, master):
self.master =master
self.frame = Frame(master)
self.master.title("Main Window")
self.frame.pack()
self.obj2 = SecondWindow
#####TypeError: TestFunction() missing 1 required positional argument: 'self'###
# here i set variables for my components
self.textlog=StringVar()
# Setting "left side" of GUI (mainly radio buttons and one resset button etc).
welcome= Label(self.frame,text="Choose the system",relief="solid",font="Arial 20").grid(row=0,sticky=E+W)
self.rd1 = Radiobutton(self.frame, text="McAfe", variable=self.var_chk, value=1,command=lambda: self.checkValue()).grid(row=1,column=0,sticky=W)
def new_window(self):
time_string = time.strftime("[%H:%M:%S]>>")
info = "Opening last graph settings"
self.newWindow = Toplevel(self.master)
myGUI = SecondWindow(self.newWindow)
self.writeToLog(time_string +"Graph settings/export was opened " +"\n" )
def writeToLog(self,insertedData):
self.log.insert("0.0",insertedData)
# here i want callfunctions>> and want to call it through objects like:
self.obj2.TestFunction()
#I need to exchange values between two classes and iam not able to do that.
#Only what i can do with this code is that i can acess all methods from Class MainWindow in class SecondWindow by app.Call_some_function with no problem.
#What i have to do to call methods in MainWindow from SecondWindow?
class SecondWindow():
def __init__(self, master):
self.master = master
self.frame = Frame(master)
self.frame.pack()
self.master.title("Graph settings")
#another buttons and more
# Here i want function which will return values,but need to load it into MainWindow class
def TestFunction(self):
print("Iam function from second class")
#and this works...
def CloseWindow(self):
time_string = time.strftime("[%H:%M:%S]>>")
info = time_string +"User exited graph window \n"
app.writeToLog(info)
self.master.destroy()
app.writeToLog(info) # HERE I WRITE INFORMATION FROM class SecondWindow into TextWidget which is placed in class MainWindow and it works.
Because app is defined in main i though.
if "name" == "name":
root = Tk()
app = MainWindow(root)
root.mainloop()