首页 > 解决方案 > 单击两个按钮时如何调用函数python

问题描述

我正在尝试在 python 中创建一些东西。我是python新手。我有这些框架,当单击一个特殊按钮时,每个框架都会打开。帧一个接一个地打开。我想在单击第一帧的按钮 1 和第二帧的按钮 1 时打开最后一页。谁能帮我?这是他的代码:

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()

标签: pythonpython-3.xbuttontkinterlabel

解决方案


推荐阅读