【问题标题】:I am trying to create buttons in tkinter我正在尝试在 tkinter 中创建按钮
【发布时间】:2016-02-20 04:26:58
【问题描述】:

我正在尝试在程序中创建按钮以将用户带到其他屏幕。我已经在标签上创建了图像,但不确定如何将它们变成可将用户带到另一个页面的功能按钮。您可以在我的代码中看到我需要的 2 个按钮是搜索报价和新报价。谢谢

from tkinter import *  
class Window(): 
def __init__(self,master): #constructor   
self.master = master       
self.master.title("Borras Roofing Quotation System") #sets title of window

self.master.geometry("2160x1440") #sets size of window  
self.master.configure(background = "white") # sets background colour of window.

self.Borras = PhotoImage(file = "Borras.Logo.PNG") #sets up image
self.BorrasLabel  = Label(self.master, image = self.Borras, bg = "white", width='1000', height= '500') #puts image onto label  
self.BorrasLabel.pack()  

self.newquote = PhotoImage(file = "Process_New_Quote_Button.PNG") #sets up image  
self.newquoteLabel = Label(self.master, image = self.newquote, bg = "white") #puts image onto label  
self.newquoteLabel.place(x = 200, y = 500) 

self.searchquote = PhotoImage(file = "Search_Current_Quote_Button.PNG") #sets up image  
self.searchquoteLabel = Label(self.master, image = self.searchquote, bg = "white") #puts image onto label  
self.searchquoteLabel.place(x = 800, y = 500)

root = Tk() 
userPassWindow = Window(root)  
root.mainloop()

【问题讨论】:

  • 您想知道如何为您的按钮提供功能吗?您是否尝试过添加commandFor example, b = Button(text="Click Me", command=function_name) ?

标签: python button tkinter window


【解决方案1】:

要在您的窗口中添加调用 open neww 的按钮,我建议您使用 Tkinter 小部件的继承来根据需要自定义新创建的窗口。

你的例子应该是:

from Tkinter import *

class search(Toplevel):
      #Just inherits TopLevel window
      #You can custumize as ou want
     def __init__(self, parent, master=None):
           Toplevel.__init__(self, master)
           #Save  parent reference to modify parent view from search view
           self.parent = parent

class quote(Toplevel):
     def __init__(self, parent, master=None):

           self.parent = parent
           Toplevel.__init__(self, master)


class Window():
      def __init__(self,master): #constructor   
           self.master = master       
           self.master.title("Borras Roofing Quotation System") #sets title of window

           self.master.geometry("400x440") #sets size of window  
           self.master.configure(background = "white") # sets background colour of window.

           self.Borras = PhotoImage(file = "Borras.Logo.PNG") #sets up image
           self.BorrasLabel  = Label(self.master, image = self.Borras, bg = "white", width='1000', height= '500') #puts image onto label  
           self.BorrasLabel.pack()  

           self.newquote = PhotoImage(file = "Process_New_Quote_Button.PNG") #sets up image  
           self.newquoteLabel = Label(self.master, image = self.newquote, bg = "white") #puts image onto label  
           self.newquoteLabel.place(x = 200, y = 500) 

           self.searchquote = PhotoImage(file = "Search_Current_Quote_Button.PNG") #sets up image  
           self.searchquoteLabel = Label(self.master, image = self.searchquote, bg = "white") #puts image onto label  
           self.searchquoteLabel.place(x = 800, y = 500)
           search_quote = Button(text="Search quote", command=self.search)
           quote = Button(text="Quote", command=self.quote)
           search_quote.pack()
           quote.pack()

      def search(self):
           #Pass parent reference 'self'
           search(self)

      def quote(self):
           quote(self)     

root = Tk() 
userPassWindow = Window(root)  
root.mainloop()

【讨论】:

    【解决方案2】:

    首先,如果您想通过单击实现任何目标,则需要使用 Button 实例,而不是 Label。如果你添加

    command = anyfunction
    

    作为按钮的参数,当按钮被按下时该函数将运行(该函数可以在类内部或外部声明,具体取决于您想要的范围)。从那里使用

    .pack_forget()
    

    .place_forget()
    

    删除项目的程序(尽管这不会删除它们)并在您的“窗口”中打包、放置或网格化新项目。如果您正在寻找要打包的实际屏幕并忘记打包,那么您应该使用 tkinter Frames。这是一个有用的链接,它将向您展示如何正确使用每个小部件:

    http://effbot.org/tkinterbook

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-04
      • 2011-12-29
      • 1970-01-01
      • 2022-11-04
      • 2016-07-31
      • 2019-07-03
      • 1970-01-01
      相关资源
      最近更新 更多