Tkinter无法将标签框放置在窗口的顶部

时间:2015-11-19 21:56:33

标签: python tkinter label

我的问题是我的第二个标签框架不会移动到中途,我期望两个框架的顶部彼此对齐,因此它们可以并排,但第二个框架位于应用程序的一半。我查看了代码并且看不到任何取代它的地方,任何人都可以帮忙吗?我知道这段代码可能不是最有效的,但它目前是一个更大的项目的构建块,我希望在我进一步创建程序时改进它,所以任何建议都会很棒。

My issue

from tkinter import *
import tkinter.ttk as ttk
import sqlite3
from random import randint

def available():
    print("works")

ref = ""
for x in range(0,9):
    ref = ref + str(randint(0,9))enter code here

    ####VARS####
    master = Tk()
    master.geometry('700x700+500+100')
    master.title('Booking System | Ref: ' + ref)
    carvar = StringVar(master)
    carvar.set("Select Option")
    typevar = StringVar(master)
    typevar.set("Select Option")
    statusvar = StringVar(master)
    statusvar.set("Select Option")
    ############

    #booking consts#
    blpadx = (0,10)
    bepadx = (0,5)


    #Column 0
    booking = LabelFrame(master, text="Booking Information", padx=5, pady=5)
    booking.grid(padx=(20,10), pady=(20,10), column=0, row=0, columnspan=2)
    dateFrom = Label(booking, text="Book From:")
    dateFrom.grid(column=0, row=0, padx=blpadx, pady=(5,0),sticky=W)
    days = Label(booking, text="Days:")
    days.grid(column=0, row=1, padx=blpadx, pady=(5,0), sticky=W)
    dateTo = Label(booking, text="Book To:")
    dateTo.grid(column=0, row=2, padx=blpadx, pady=(5,0), sticky=W)
    rooms = Label(booking, text="Rooms:")
    rooms.grid(column=0, row=3, padx=blpadx, pady=(5,0), sticky=W)
    car = Label(booking, text="Car:")
    car.grid(column=0, row=4, padx=blpadx, pady=(5,0), sticky=W)
    stayType = Label(booking, text="Stay Type:")
    stayType.grid(column=0, row=5, padx=blpadx, pady=(5,0), sticky=W)
    bookStatus = Label(booking, text="Booking Status:")
    bookStatus.grid(column=0, row=6, padx=blpadx, pady=(5,0), sticky=W)
    bHrRule = ttk.Separator(booking, orient=HORIZONTAL)
    bHrRule.grid(column=0, row=7, columnspan=2, sticky=EW,pady=(15,15))
    availableInfo = Label(booking, text="Click Check Availability to show the room types\n available between the selected dates.")
    availableInfo.grid(column=0, row=8, padx=blpadx, pady=(0,10), columnspan=2)
    available = Button(booking, text="Check Availability", command=available)
    available.grid(column=0, row=9, columnspan=2, pady=(0,15))



    #Column 1
    dateFromEntry = Entry(booking)
    dateFromEntry.grid(column=1, row=0, padx=bepadx, pady=(5,0))
    dateFromEntry.insert(END, "DD/MM/YYYY") 
    daysEntry = Entry(booking)
    daysEntry.grid(column=1, row=1, padx=bepadx, pady=(5,0))
    dateToEntry = Entry(booking)
    dateToEntry.grid(column=1, row=2, padx=bepadx, pady=(5,0))
    dateToEntry.insert(END, "DD/MM/YYYY") 
    roomsEntry = Entry(booking)
    roomsEntry.grid(column=1, row=3, padx=bepadx, pady=(5,0))
    carEntry = OptionMenu(booking,carvar, "No", "Yes")
    carEntry.config(width=15)
    carEntry.grid(column=1, row=4, padx=bepadx, pady=(5,0))
    stayType = OptionMenu(booking,typevar, "Self Catering", "All-Inclusive")
    stayType.config(width=15)
    stayType.grid(column=1, row=5, padx=bepadx, pady=(5,0))
    bookStatus = OptionMenu(booking,statusvar, "placeholder", "same")
    bookStatus.grid(column=1, row=6, padx=bepadx, pady=(5,0))
    bookStatus.config(width=15)

    #Column 2
    customer = LabelFrame(master, text="Customer Details", padx=5, pady=5)
    customer.grid(padx=(0,0), pady=(0,0), column=2, row=0)
    title = Label(customer, text="Book From:")
    title.grid(column=2, row=0, padx=blpadx, pady=(5,0),sticky=W)

    #Shoop De Loop
    mainloop()

1 个答案:

答案 0 :(得分:0)

当您添加labelframe customer时,您只使用sticky W。这意味着它会粘在已经给出的空间的左侧,而不是顶部和底部。如果你想让它粘在它给定空间的顶部,你需要包含“N”。

这是一个建议,让这些问题更容易被发现将所有布局代码组合在一起。而不是小部件,布局,小部件,布局,将所有小部件放在一起,然后将所有布局放在一起。

我还建议将所有兄弟姐妹一起布置。因此,首先布置labelframes,然后布置每个框架中的其他小部件。

例如:

#Column 0
booking = LabelFrame(master, text="Booking Information", padx=5, pady=5)
dateFrom = Label(booking, text="Book From:")
days = Label(booking, text="Days:")
dateTo = Label(booking, text="Book To:")
rooms = Label(booking, text="Rooms:")
car = Label(booking, text="Car:")
stayType = Label(booking, text="Stay Type:")
bookStatus = Label(booking, text="Booking Status:")
bHrRule = ttk.Separator(booking, orient=HORIZONTAL)
availableInfo = Label(booking, text="Click Check Availability to show the room types\n available between the selected dates.")
available = Button(booking, text="Check Availability")

#Column 1
dateFromEntry = Entry(booking)
dateFromEntry.insert(END, "DD/MM/YYYY") 
daysEntry = Entry(booking)
dateToEntry = Entry(booking)
dateToEntry.insert(END, "DD/MM/YYYY") 
roomsEntry = Entry(booking)
carEntry = OptionMenu(booking,carvar, "No", "Yes")
carEntry.config(width=15)
stayType = OptionMenu(booking,typevar, "Self Catering", "All-Inclusive")
stayType.config(width=15)
bookStatus = OptionMenu(booking,statusvar, "placeholder", "same")
bookStatus.config(width=15)

#Column 2
customer = LabelFrame(master, text="Customer fooDetails", padx=5, pady=5)
title = Label(customer, text="Book From:")

booking.grid(padx=(20,10), pady=(20,10), column=0, row=0, columnspan=2)
customer.grid(padx=(0,0), pady=(0,0), column=2, row=0, sticky="N")

# booking
dateFrom.grid(column=0, row=0, padx=blpadx, pady=(5,0),sticky=W)
dateFromEntry.grid(column=1, row=0, padx=bepadx, pady=(5,0))
dateTo.grid(column=0, row=2, padx=blpadx, pady=(5,0), sticky=W)
days.grid(column=0, row=1, padx=blpadx, pady=(5,0), sticky=W)
rooms.grid(column=0, row=3, padx=blpadx, pady=(5,0), sticky=W)
car.grid(column=0, row=4, padx=blpadx, pady=(5,0), sticky=W)
stayType.grid(column=0, row=5, padx=blpadx, pady=(5,0), sticky=W)
bookStatus.grid(column=0, row=6, padx=blpadx, pady=(5,0), sticky=W)
bHrRule.grid(column=0, row=7, columnspan=2, sticky=EW,pady=(15,15))
availableInfo.grid(column=0, row=8, padx=blpadx, pady=(0,10), columnspan=2)
available.grid(column=0, row=9, columnspan=2, pady=(0,15))

daysEntry.grid(column=1, row=1, padx=bepadx, pady=(5,0))
dateToEntry.grid(column=1, row=2, padx=bepadx, pady=(5,0))
roomsEntry.grid(column=1, row=3, padx=bepadx, pady=(5,0))
carEntry.grid(column=1, row=4, padx=bepadx, pady=(5,0))
stayType.grid(column=1, row=5, padx=bepadx, pady=(5,0))
bookStatus.grid(column=1, row=6, padx=bepadx, pady=(5,0))

# customer
title.grid(column=2, row=0, padx=blpadx, pady=(5,0))

这不一定完全我将如何做到这一点,但重点是逻辑分组您的代码,以便您可以只关注布局,或只关注小部件对象,而不必处理它一切都在同一时间。