gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:14:22 27 4
gpt4 key购买 nike

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

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

最佳答案

您可以使用继承并使每个窗口成为 Toplevel 小部件的子窗口。

示例:

from Tkinter import *

class search(Toplevel):
# Just inherits TopLevel window
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") #puts image onto label
self.BorrasLabel.place(x = 10, y = 50)

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 = 150, y = 50)

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 = 300, y = 50)
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()

关于python - 我正在尝试在 tkinter 中创建按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33780002/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com