0

I am new to python and GUI-Thinker. I'm learning about how to switch windows on GUI using Tkinker as UI, and python as a programming language.

I followed this guideline Switch between two frames in tkinter to switch frames in thinker and it worked.

Then, I'm trying to show widgets and hide all other widgets using bind("<<ComboboxSelected>>". But when I choose Monthly option, widgets as I set under DeleteOptions condition didn't show up. The same thing to Period option, widgets as I set under DeleteOptions condition didn't show up.

My Code:

from tkinter import *
import tkinter as tk
from tkinter import ttk
from tkinter import font as tkfont
from tkcalendar import DateEntry

class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")

        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        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 (StartPage, PageOne):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is the start page", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)

        button1 = tk.Button(self, text="Go to Page One",
                            command=lambda: controller.show_frame("PageOne"))
        button1.pack()
        
class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is page 1", font=controller.title_font)
        label.grid()

        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.grid()

        def DeleteOptions():
            if SVcmb_Delete.current()=="Monthly":
                HideDeleteOptions()
                lblMonthly.grid(row=4)
                SVcmb_Monthly.grid(row=4, column=1)

            if SVcmb_Delete.current()=="Period":
                HideDeleteOptions()
                lblFrom.grid(row=4)
                txtFrom.grid(row=4, column=1)
                lblTo.grid(row=5)
                txtTo.grid(row=5, column=1)

        lblSV_Search=tk.Label(self,text="Delete by")
        lblSV_Search.grid(row=3)

        SVcmb_Delete=ttk.Combobox(self,state="readonly",justify=CENTER,font=("times new roman",15))
        SVcmb_Delete["values"]=("Select","Monthly","Period")
        SVcmb_Delete.grid(row=3,column=1)
        SVcmb_Delete.bind("<<ComboboxSelected>>", lambda event:DeleteOptions)

        def HideDeleteOptions():
            lblMonthly.grid_forget()
            SVcmb_Monthly.grid_forget()
            lblFrom.grid_forget()
            txtFrom.grid_forget()
            lblTo.grid_forget()
            txtTo.grid_forget()

        lblMonthly=tk.Label(self,text="Monthly")
        
        SVcmb_Monthly=ttk.Combobox(self,state="readonly",values=[1,2,3],justify=CENTER)
        
        lblFrom=tk.Label(self,text="From")
        
        txtFrom=DateEntry(self,selectmode='day',date_pattern='mm/dd/y')
        
        lblTo=tk.Label(self,text="To")
        
        txtTo=DateEntry(self,selectmode='day',date_pattern='mm/dd/y')
        
if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

On page one. I'm looking for a way like when I chose Monthly option, all widgets under DeleteOptions condition show up and all widgets of Period option are hidden as I set under DeleteOptions condition. The same thing to Period option, all widgets under DeleteOptions condition show up and all widgets of Monthly option are hidden as I set under DeleteOptions condition.

Please help me. Thank you so much.

Cristian
  • 155
  • 8

1 Answers1

0

There are two problems with your code. This first is with your lambda expression:

SVcmb_Delete.bind("<<ComboboxSelected>>", lambda event:DeleteOptions)

is wrong because it doesn't actually call the DeleteOptions() function the way it's written.

Instead, do it like this:

SVcmb_Delete.bind("<<ComboboxSelected>>", lambda event: DeleteOptions())

The second problem is with your use of the SVcmb_Delete.current() method in the DeleteOptions() function because it returns the index of the the current entry text, not the entry text itself. To obtain that, you should use the get() method. With that correction made the function should look like something like this:

    def DeleteOptions():
        if SVcmb_Delete.get() == "Monthly":
            HideDeleteOptions()
            lblMonthly.grid(row=4)
            SVcmb_Monthly.grid(row=4, column=1)

        if SVcmb_Delete.get() == "Period":
            HideDeleteOptions()
            lblFrom.grid(row=4)
            txtFrom.grid(row=4, column=1)
            lblTo.grid(row=5)
            txtTo.grid(row=5, column=1)

Lastly I also strongly suggest that you read and start following the PEP 8 - Style Guide for Python Code to make your code more readable.

martineau
  • 119,623
  • 25
  • 170
  • 301