我想在tkinter中创建按钮

时间:2015-11-18 12:24:20

标签: python button tkinter window

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

2 个答案:

答案 0 :(得分:1)

要在Windows中添加调用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()

答案 1 :(得分:0)

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

command = anyfunction

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

.pack_forget()

.place_forget()

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

http://effbot.org/tkinterbook