I'm trying to build software mimicking software installation page. The first page will ask on the installation directory. And when the "Next" button is clicked, the frame switched to another page (not sure whether the previous frame is destroyed) - but, the installation directory that is chosen by user still stored in a variable and accessible.
The problem with my code, I cannot access the variable outside the def and class. I combined several codes from different sources. I tried using var.get(), var.set(). Make into global - but some ppl said it's not a good habit to make everything global. Appreciate if anyone can help.
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()