[英]Tkinter - Button on new window
我想在新窗口中添加一個按鈕,但我對此有疑問。 我是在函數內部執行此操作,就像在標簽中執行此操作,還是在我在下方保留的空間中執行此操作,僅用於按鈕?
遵循以下代碼:
from tkinter import *
ftela = Tk()
ftela.state("zoomed")
ftela.title("OMNIA v0")
menu = Menu(ftela)
ftela.config(menu=menu)
##-----------------##
## FUNÇÕES/DEF ##
##-----------------##
def new_window():
newWindow = Toplevel()
newWindow.geometry("800x600+275+75")
newWindow.resizable(height=False, width=False)
newWindow = LabelFrame(newWindow, text="sex")
newWindow.place(x=10, y=10, width=675, height=400)
##-----------------##
## LABELS ##
##-----------------##
mylabel = LabelFrame(ftela, text="teste")
mylabel.place(x=10, y=10, width=300, height=150)
l_teste = Label(ftela, text="teste")
l_teste.place(x=15, y=30)
e_teste = Entry(ftela)
e_teste.place(x=100, y=30)
##-----------------##
## SUBMENU ##
##-----------------##
subMenu1=Menu(menu, tearoff=0)
menu.add_cascade(label="Cadastro", menu=subMenu1)
subMenu1.add_command(label="Empresas", command=new_window)
subMenu1.add_command(label="Usuários")
subMenu1.add_separator()
subMenu1.add_command(label="Bancos/Caixas")
subMenu1.add_command(label="Contas")
##-----------------##
## BOTOES ## <<< here **
##-----------------##
bt = Button()
bt.place(x=100, y=100, width=100, height=100)
ftela.mainloop()
是的,只需在創建新窗口的同一函數中添加Button
並指定newWindow
作為其父窗口。 (創建任何小部件時,都會創建父子關系。例如,如果將文本標簽放置在框架內,則框架是標簽的“父級”。)
這意味着做這樣的事情:
##-----------------##
## FUNÇÕES/DEF ##
##-----------------##
def new_window():
newWindow = Toplevel()
newWindow.geometry("800x600+275+75")
newWindow.resizable(height=False, width=False)
labelFrame = LabelFrame(newWindow, text="sex")
labelFrame.place(x=10, y=10, width=675, height=400)
##-----------------##
## BOTOES ##
##-----------------##
btn = Button(newWindow, text="My Button") # HERE
btn.place(x=100, y=100, width=100, height=100)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.