所以...我有三个不同的文件,每个文件都包含一个类,一个是父类,两个是子类。我想做的是将一个变量从child1发送到child2(在这种情况下,将变量serialNumber从scanPage传递给waitForTestPage),我将提供一些我尝试执行的代码:
主要:
import tkinter as tk
from tkinter import font as tkfont
import os.path
from scanPage import ScanPage
from waitForTestPage import WaitForTestPage
PATH = os.path.realpath(__file__)
PATH = os.path.dirname(os.path.dirname(PATH))
class MainFrame(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.wm_title('Test')
self.geometry('900x500')
self.resizable(0, 0)
self.titleFont = tkfont.Font(family='Helvetica', size=18,
weight='bold', slant='italic')
container = tk.Frame(self)
container.pack(side='top', fill='both', expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for frameNumber in (
ScanPage, WaitForTestPage,
):
pageName = frameNumber.__name__
frame = frameNumber(parent=container, controller=self)
self.frames[pageName] = frame
frame.grid(row=0, column=0, sticky='nsew')
self.showFrame('ScanPage')
def showFrame(self, pageName):
'''Show a frame for the given page name'''
frame = self.frames[pageName]
frame.tkraise()
if __name__ == '__main__':
APP = MainFrame()
APP.mainloop()
ScanPage:
import tkinter as tk
import os
PATH = os.path.realpath(__file__)
PATH = os.path.dirname(os.path.dirname(PATH))
class ScanPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
def serialNumberCallBack():
serialNumber = self.entryValue.get()
print(serialNumber)
self.entry.delete(0, 'end')
tmpFilesFolder = PATH + '/tmpFiles/'
if not os.path.exists(tmpFilesFolder):
os.mkdir(tmpFilesFolder)
dirName = tmpFilesFolder + serialNumber
if not os.path.exists(dirName):
os.mkdir(dirName)
self.entryValue = tk.StringVar()
self.message = tk.Label(
self,
text='Scan the serial number',
font='Helvetica 27'
)
self.message.pack()
self.entry = tk.Entry(self, textvariable=self.entryValue, width=20, font=('Ubunto', 24))
self.entry.focus()
self.entry.pack()
self.message2 = tk.Label(
self,
text='and click on "NEXT"',
font='Helvetica 27'
)
self.message2.pack()
self.button = tk.Button(
self, text='NEXT', width=29, font='Helvetica 15',
command=lambda: [serialNumberCallBack(), controller.showFrame('WaitForTestPage')]
)
self.button.pack(pady=50)
WaitForTestPage:
import tkinter as tk
import os, fnmatch
from config import PATH_TO_SEARCH_FROM
from scanPage import *
import shutil
PATH = os.path.realpath(__file__)
PATH = os.path.dirname(os.path.dirname(PATH))
class WaitForTestPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
def waitForTestCallBack():
print(serialNumber)
self.message = tk.Label(
self,
text='Certify that the test is completed and press "NEXT"',
font='Helvetica 27'
)
self.message.pack()
self.button = tk.Button(
self, text='NEXT', width=29, font='Helvetica 15',
command=lambda: [waitForTestCallBack(), controller.showFrame('ScanPage')]
)
self.button.pack(pady=50)