Brief overview of the program:
- When launching script, the dialog window is appearing and asking you to select directory path (class
MyApp)
- When selected - one needs to push the "Start Monitoring" button. This action withdraws the main window (
root) and initializes the container class FrameContainer consisting of two frames, namely PageOne and PageTwo (see corresponding classes).
- Button
Back Home on Page One should destroy the instance of the class FrameContainer and show the dialog window again. This action is achieved using methods update() and deiconify().
- Selection of any of the radio buttons on
Page One should change the value of variable rb_indx - it is achieved using the get() method of IntVar.
Problems:
- Radio buttons are initialized in wrong way. The
var.set(1) should highlight the 'var1' button, however in my case are highlighted var2 and var3.
- The
get() method of IntVar does not update the value of the variable rb_indx when selecting a radio button. print rb_indx always gives 1
- When closing
PageOne window by the [x] (in corner of the screen), the dialog window is not appearing, however is expected...
The code:
# -*- coding: utf-8 -*-
try:
# Python2
import Tkinter as Tk
from Tkinter import IntVar
from tkFileDialog import askdirectory
except ImportError:
# Python3
import tkinter as Tk
from tkinter import IntVar
from tkinter.filedialog import askdirectory
LARGE_FONT=("Verdana", 12)
########################################################################
class FrameContainer(Tk.Tk):
def __init__(self, parent):
self.original_frame = parent
Tk.Tk.__init__ (self)
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 F in (PageOne, PageTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(PageOne)
def show_frame (self, cont):
frame=self.frames[cont]
frame.tkraise()
def onClose(self):
self.destroy()
self.original_frame.show()
class PageOne(Tk.Frame):
def __init__(self, parent, controller):
Tk.Frame.__init__(self, parent)
label = Tk.Label(self, text="Page One", font=LARGE_FONT)
label.pack(pady=10, padx=10)
button1=Tk.Button(self, text="Back to Home", command=lambda: controller.onClose())
button1.pack()
button2=Tk.Button(self, text="Page Two", command=lambda: controller.show_frame(PageTwo))
button2.pack()
def get_button_indx():
global rb_indx
rb_indx=var.get()
print rb_indx
var.set(1)
names_tuple = ('var1', 'var2', 'var3')
for col_numb in range (1,len(names_tuple)+1):
radio = Tk.Radiobutton(self, text=names_tuple[col_numb-1], variable=var, value=col_numb, command=get_button_indx)
radio.pack()
class PageTwo(Tk.Frame):
def __init__(self, parent, controller):
Tk.Frame.__init__(self, parent)
label = Tk.Label(self, text="Page Two", font=LARGE_FONT)
label.pack(pady=10, padx=10)
button1=Tk.Button(self, text="Back to Home", command=lambda: controller.onClose())
button1.pack()
button2=Tk.Button(self, text="Page One", command=lambda: controller.show_frame(PageOne))
button2.pack()
class MyApp(object):
def __init__(self, parent):
self.root = parent
self.root.title("Start page")
self.frame = Tk.Frame(parent)
self.frame.pack()
def choosedir():
global path_usr_var
path_usr_var = askdirectory()
print path_usr_var
button1 = Tk.Button(self.frame, text="Select Directory", command=choosedir)
button1.pack(pady=10, padx=10)
button2 = Tk.Button(self.frame, text="Start Monitoring", command=self.openFrame)
button2.pack(pady=10, padx=10)
def hide(self):
self.root.withdraw()
def openFrame(self):
self.hide()
firstFrame = FrameContainer(self)
def show(self):
self.root.update()
self.root.deiconify()
########################################################################
if __name__ == "__main__":
root = Tk.Tk()
root.geometry("800x600")
var = IntVar()
app = MyApp(root)
root.mainloop()