繁体   English   中英

我正在尝试在 tkinter 中创建按钮

[英]I am trying to create buttons in tkinter

我正在尝试在程序中创建按钮以将用户带到其他屏幕。 我已经在标签上创建了图像,但不确定如何将它们变成将用户带到另一个页面的功能按钮。 您可以在我的代码中看到我需要的 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()

要在您的窗口中添加调用 open neww one 的按钮,我建议您使用 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()

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

command = anyfunction

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

.pack_forget()

或者

.place_forget()

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

http://effbot.org/tkinterbook

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM