【问题标题】:Button behind the frame框架后面的按钮
【发布时间】:2017-01-13 14:52:54
【问题描述】:

在编码方面,我几乎是一个完整的初学者。我一直在使用 python 和 tkinter GUI 来编写预订系统。最终需要集成数据库,但我正在尝试首先设置 GUI。我的代码有点乱,但我现在并不担心。 我试图在框架前面设置一个按钮,但它留在后面,有什么方法可以阻止它?

这是我的代码...

def home_button():
    home_title = Button(book, text='HOME', background='#DEEBF7', activebackground='#DEEBF7', width=15, border=0, font = ("Helvetica 15 italic"))
    home_title.place(x=423, y=81)

def bookings_button():
    bookings_title = Label(book, text='BOOKINGS', background='#DEEBF7', font = ("Helvetica 15 italic"))
bookings_title.place(x=475, y=85)
new_booking = Button(book, text='NEW BOOKING', width=20, height=2, background='#87B7E2')
new_booking.place(x=160, y=170)

def students_button():
    students_title = Label(book, text='STUDENTS', background='#DEEBF7', font = ("Helvetica 15 italic"))
    students_title.place(x=475, y=85)

##       GUI        ####
from tkinter import *

root = Tk()
#create the root window
book = Frame(root)
book.grid()

root.title("Home")
root.geometry("850x550")
root.configure(background='#DEEBF7')
backg = Button(book, height=850, width=550, background='#DEEBF7',
    activebackground='#DEEBF7', border=0)
backg.pack()
#modify the windows size, colour and the size of the grid

logo1 = Button(book, width=175, height=117, border=0, background='#DEEBF7', activebackground='#DEEBF7', command=home_button)
logo = PhotoImage(file='Logo1.gif')
logo1.image = logo
logo1.configure(image=logo)
logo1.place(x=50, y=20)

title = Label(book, text = "GRAHAM'S SCHOOL OF MOTORING", background = '#DEEBF7', font = ("Helvetica 24 bold"))
title.place(x=250, y=40)

frame=Frame(root, width=551, height=384, background='#000000')
frame.place(x=250, y=135)
frame=Frame(root, width=547, height=380, background='#DEEBF7', borderwidth=5)
frame.place(x=252, y=137)

home = Button(book, text = "HOME", height=2, width=20, background='#5F85CD',
    borderwidth=5, activebackground='#A2C7E8', relief='raised', command=home_button)
home.place(x=60, y=150)


bookings = Button(book, text="BOOKINGS", height=2, width=20, background='#5F85CD', borderwidth=5, activebackground='#A2C7E8', relief='raised', command=bookings_button)
bookings.place(x=60, y=235)


student = Button(book, text="STUDENTS", height=2, width=20, background='#5F85CD',
    borderwidth=5, activebackground='#A2C7E8', relief='raised', command=students_button)
student.place(x=60, y=320)


back = Button(book, text="BACK", height=2, width=20, background='#FF5559',
    borderwidth=5, activebackground='#BF230A', relief='raised')
back.place(x=45, y=450)

root.mainloop()
#kick off the window's event-loop

【问题讨论】:

  • python-course.eu Tkinter tutorial 中所述,您应该永远在同一个主窗口中混合使用packgridplace 几何管理器。
  • 哪个按钮?哪个框架?
  • @PM2Ring:小心你的术语。您可以将它们组合在同一个窗口中(假设窗口有多个框架),而不是在同一个小部件中。
  • @BryanOakley 好的。但是,OP 在book 框架中混合了.packplace

标签: python button tkinter frame


【解决方案1】:

为了将按钮放在框架顶部,我更改了按钮的主小部件:

我换了

new_booking = Button(book, text='NEW BOOKING', width=20, height=2, background='#87B7E2')

通过

new_booking = Button(root, text='NEW BOOKING', width=20, height=2, background='#87B7E2')

【讨论】:

  • 感谢您的建议,我后来发现了这个错误并像您一样更改了它,但还是谢谢您:)
猜你喜欢
  • 2021-11-22
  • 2011-11-12
  • 1970-01-01
  • 2017-01-20
  • 2011-07-12
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
  • 2018-05-12
相关资源
最近更新 更多