1

I am trying to create something in python. I am new in python. I have these frames, each frame will open when a special button is clicked. frames are opened one after each other. I want to open the final page when button 1 of first frame and button 1 of second frame are clicked. can anyone help me? Here is he code:

import tkinter as tk
LARGE_FONT="Verdana",16,"Bold"
MEDIUM_FONT="Verdana Bold"
class SeaofBTCapp(tk.Tk):
  def __init__(self,*args,**kwargs):
    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 (StartPage,firstPage,secondPage, thirdPage,):
        frame=F(container,self)
        self.frames[F]=frame
        frame.grid(row=0,column=0,sticky="nsew")
    self.show_frame(StartPage)
def show_frame(self,cont):
    frame=self.frames[cont]
    frame.tkraise()
def qf(param):
   print(param)
class StartPage(tk.Frame):
   def __init__(self,parent,controller):
    tk.Frame.__init__(self,parent)
    button1=tk.Button(self,text="START",
    command=lambda : controller.show_frame(firstPage))
    button1.pack()
def OnButtonClick( button_id):
    if (button_id == 11 and button_id==21):
    #if button 11 and 21 were clicked I want to show the final page frame not thiird page frame
class firstPage(tk.Frame):
    def __init__(self,parent,controller):
    tk.Frame.__init__(self,parent)
    label1=tk.Label(self,width=50,text="Label one:",bg="#362458",fg="#FFFFFF")
    label1.grid(row=2,pady=10,padx=10)
    button1=tk.Button(self,text="Button 1",width=8,bg="#362458",fg="#FFFFFF",borderwidth=2, relief="solid",
    command=lambda : [controller.show_frame(secondPage),OnButtonClick(11)])
    button1.grid(row=4,pady=10,padx=10)
    button2=tk.Button(self,text="Button 2",width=8,bg="#362458",fg="#FFFFFF",borderwidth=2, relief="solid",
    command=lambda : [controller.show_frame(secondPage),OnButtonClick(12)])
    button2.grid(row=6,pady=10,padx=10)
 class secondPage(tk.Frame):
  def __init__(self,parent,controller):
    tk.Frame.__init__(self,parent)
    label1=tk.Label(self,width=50,text="Label 2",bg="#362458",fg="#FFFFFF")
    label1.grid(row=2,pady=10,padx=10)
    button1=tk.Button(self,text="Button 1",width=8,bg="#362458",fg="#FFFFFF",borderwidth=2, relief="solid",
    command=lambda : [controller.show_frame(thirdPage),OnButtonClick(21)])
    button1.grid(row=4,pady=10,padx=10)
    button2=tk.Button(self,text="Button 2",width=8,bg="#362458",fg="#FFFFFF",borderwidth=2, relief="solid",
    command=lambda : [controller.show_frame(thirdPage),OnButtonClick(22)])
    button2.grid(row=6,pady=10,padx=10)
class thirdPage(tk.Frame):
    def __init__(self,parent,controller):
    tk.Frame.__init__(self,parent)
    label1=tk.Label(self,width=50,text="Label 3",bg="#362458",fg="#FFFFFF")
    label1.grid(row=2,pady=10,padx=10)
    button1=tk.Button(self,text="Button 1",width=8,bg="#362458",fg="#FFFFFF",borderwidth=2, relief="solid",
    command=lambda : [controller.show_frame(FinalPage),OnButtonClick(31)])
    button1.grid(row=4,pady=10,padx=10)
    button2=tk.Button(self,text="Button 2",width=8,bg="#362458",fg="#FFFFFF",borderwidth=2, relief="solid",
    command=lambda : [controller.show_frame(FinalPage),OnButtonClick(32)])
    button2.grid(row=6,pady=10,padx=10
 class FinalPage(tk.Frame):
    def __init__(self,parent,controller):
    tk.Frame.__init__(self,parent)
    button1=tk.Button(self,text="Click here to go to the start page",bg="#00BF89",fg="white",
    command=lambda : controller.show_frame(StartPage))        
    button1.pack()
  app=SeaofBTCapp() 
  app.mainloop()
Arsaa
  • 87
  • 1
  • 1
  • 4
  • Hi new contributor! It seems this is very early in your involvement with GUI. GUI processes are split into 2 major fractions, widgets and events. Widgets are classes that represent anything you see or should do something (frame,window, button,label) and events are stuck in another thread in endless loop triggered when something happens. – Danilo Nov 19 '19 at 16:20
  • So you can acces that button via its constructor as shown in example of MegaIng : https://stackoverflow.com/questions/48245132/how-to-handle-a-click-button-event-in-python-module-tkinter via `labda` function. Or they can be accesed by event handlers : https://www.python-course.eu/tkinter_events_binds.php – Danilo Nov 19 '19 at 16:21
  • In short, if you wan't to use an combined events (as you seem to do) you need 2 things: 1) Way to store both values (to avoid threading issues) 2) time that you are willing to measure it from. If both buttons are clicked 1 hour appart, should the function activate , or does it need more narrower time span ? And you need to store both `button_ids` separately, and then measure the time between them - since `button_id` can't hold 2 values. One of those events will arrive before the other, and you need to record both of them. – Danilo Nov 19 '19 at 16:48
  • How can I do that? – Arsaa Nov 19 '19 at 17:02
  • I believe that soon people with more rep will come and give you better examples, but you would need an independent variables in your class that hold current time when `button_id` is either 11 or 21 https://stackoverflow.com/questions/415511/how-to-get-the-current-time-in-python , and if clause that when 11 or 21 occurred, checks if time is stored in that variable, if it is stored and time difference is less than X, do that function, else clear the variable and do nothing. – Danilo Nov 19 '19 at 17:06
  • So that variable could be in form of tuple `(button_id, current_time)` where you could check if the same button was hit twice. So logic needed for activation of your function would be, `if (var[0] == 21 and button_id==11) or ( var[0] == 11 and button_id==21) : check for time difference; else if (( var[1] - current_time ) > X ): var = None; else : pass; `. Something along of those lines. – Danilo Nov 19 '19 at 17:12
  • buttons 11 and 21 must be clicked one after the other, or it can be clicked for example 11 -> 22 -> 21 and then show final page? – ncica Nov 19 '19 at 17:43
  • @ncica button 11 and 21 – Arsaa Nov 19 '19 at 17:51
  • @Arsaa one after the other? – ncica Nov 19 '19 at 17:51
  • @ncica yes one after the other – Arsaa Nov 19 '19 at 17:53

0 Answers0