【发布时间】:2019-11-04 03:45:05
【问题描述】:
我正在尝试构建模拟软件安装页面的软件。第一页会询问安装目录。并且当点击“下一步”按钮时,框架切换到另一个页面(不确定上一个框架是否被破坏)-但是,用户选择的安装目录仍然存储在变量中并且可以访问。
我的代码有问题,我无法访问 def 和 class 之外的变量。我结合了来自不同来源的几个代码。我尝试使用 var.get()、var.set()。全球化——但有些人说让一切全球化不是一个好习惯。如果有人可以提供帮助,请不胜感激。
import tkinter as tk
from tkinter import *
from tkinter import font as tkfont
import os
from PIL import Image, ImageTk
from tkinter import filedialog
class MainPage(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self._frame = None
self.switch_frame(StartPage)
def switch_frame(self, frame_class):
new_frame = frame_class(self)
if self._frame is not None:
self._frame.destroy()
self._frame = new_frame
self._frame.pack()
class StartPage(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.master.title("Starting Point")
Heading = tkfont.Font(family = 'Helvetica', size = 16, weight = 'bold')
logo = Image.open("xxx.jpg")
logo_pic = ImageTk.PhotoImage(logo)
logo_label = tk.Label(self, image=logo_pic)
logo_label.image = logo_pic
logo_label.pack(pady = 30)
button1 = tk.Button(self, text="Page1", font = Heading, height = 3, width = 15 , command=lambda: master.switch_frame(Page1))
button1.pack(pady = 10)
button2 = tk.Button(self, text="Page2", font = Heading, height = 3, width = 15 , command=lambda: master.switch_frame(Page2))
button2.pack(pady = 10)
button3 = tk.Button(self, text="Page3", font = Heading, height = 3, width = 15 , command=lambda: master.switch_frame(Page3))
button3.pack(pady = 10)
class Page1(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.master.title("Page1")
Heading = tkfont.Font(family = 'Helvetica', size = 20, weight = 'bold')
Content = tkfont.Font(family = 'Helvetica', size = 16)
try:
outdir = os.makedirs(os.path.join(os.path.expanduser('~')+'/Result/'))
except:
pass
outdir = os.path.join(os.path.expanduser('~')+'/Result/')
label = tk.Label(self, text="Page1", font= Heading)
label.pack(pady = 10)
label1 = tk.Label(self, text="Please select single file or a folder", font = Content)
label1.pack(pady = 10)
processing = tk.IntVar()
processing.set(1)
#filelist = tk.StringVar()
#indir = tk.StringVar()
#OCRdir = tk.StringVar()
singleButton = tk.Radiobutton(self, text="Single File", variable=processing, value=1, command = self.getFileName)
singleButton.pack(pady = 10)
folderButton = tk.Radiobutton(self, text="Folder", variable=processing, value=2, command = self.getDirectory)
folderButton.pack(pady = 10)
label2 = tk.Label(self, text="Please set the directory for OCR", font = Content)
label2.pack(pady = 10)
OCRButton = tk.Button(self, text="Browse", command = self.getOCRdir)
OCRButton.pack(pady = 10)
prevbutton = tk.Button(self, text="Back to Home",
command=lambda: master.switch_frame(StartPage))
prevbutton.pack(side = 'left')
print(processing)
print(filelist)
print(indir)
print(OCRdir)
#processbutton = tk.Button(self, text="Next", command=self.Next)
#processbutton.pack(side = 'right')
def getFileName(self):
filename = tk.filedialog.askopenfilename(initialdir = "/", title = 'Please select a file')
filelist.set(filename)
if filelist.endswith('.xml') or filelist.endswith('.tif'):
indir = os.path.splitext(filelist)[0].split('/')[0:len(os.path.splitext(filelist)[0].split('/'))-1]
indir = '/'.join(indir)
filelist = [os.path.splitext(filelist)[0].split('/')[-1].upper()]
else:
filelist = 0
indir = 0
processing.set(1)
def getDirectory(self):
directory = tk.filedialog.askdirectory(initialdir="/", title='Please select a directory')
indir.set(directory)
files = os.listdir(indir)
filelist = []
for filename in files:
if filename.endswith('.xml') or filename.endswith('.tif'):
filelist.append(filename)
filelist = list(set([os.path.splitext(x)[0].split('/')[-1].upper() for x in filelist]))
processing.set(2)
def getOCRdir(self):
OCR = tk.filedialog.askdirectory(initialdir="/", title='Please select a directory')
OCRdir.set(OCR)
#def startextract(self):
#os.system('python Datafile.py')
def main():
app = MainPage()
w, h = app.winfo_screenwidth(), app.winfo_screenheight()
app.geometry("%dx%d+0+0" % (w, h))
app.mainloop()
if __name__ == '__main__':
main()
【问题讨论】:
标签: python macos tkinter jupyter-notebook