我正在尝试编写一个自动点唱机,但是我还处于早期阶段,在使用anchor时遇到了一个问题。
这是我的代码;
from tkinter import *
from tkinter import messagebox as box
def main_menu():
window = Tk()
window.title('Juke Box')
window.geometry('800x480')
window.configure(background = 'black')
label = Label(window, text = 'Juke-Box', fg = 'light green', bg = 'black', font = (None, 30), height = 2)
label.pack(side = TOP)
Jam = Button(window, text = 'The Jam', width = 25, height = 2)
Jam.pack(pady = 10, padx = 25, anchor = 'w')
Roses = Button(window, text = 'The Stone Roses', width = 25, height = 2)
Roses.pack(pady = 10, padx = 25, anchor = 'w')
Smiths = Button(window, text = 'The Smiths', width = 25, height = 2)
Smiths.pack(pady = 10, padx = 25, anchor = 'w')
Wedding = Button(window, text = 'The Wedding Pressent', width = 25, height = 2)
Wedding.pack(pady = 10, padx = 25, anchor = 'w')
Blondie = Button(window, text = 'Blondie', width = 25, height = 2)
Blondie.pack(pady = 10, padx = 25, anchor = 'w')
Clash = Button(window, text = 'Clash', width = 25, height = 2)
Clash.pack(pady = 10, padx = 25, anchor = 'w')
Madness = Button(window, text = 'Madness', width = 25, height = 2)
Madness.pack(pady = 10, padx = 25, anchor = 'n')
Pistols = Button(window, text = 'The Sex Pistols', width = 25, height = 2)
Pistols.pack(pady = 10, padx = 25, anchor = 'n')
window.mainloop()
main_menu()我的问题是,当我在左边堆叠相册时,我已经用完了空间,我想开始往下堆叠它们,所以我使用了anchor = 'n',但是它把它们都移到了底部。
请谁能帮我找到一种方法,从顶部开始堆叠我的相册中间。
我使用的是python 3。
发布于 2016-03-05 18:50:42
对于你想要做的事情,grid几何结构似乎是一个更好的主意,你可以显式地编码项目的位置,而不是让pack几何图形隐式地决定它将去哪里。另一个改进是在您放置每个专辑Button的地方有一个Frame。我已经编辑了你的代码:
from tkinter import *
from tkinter import messagebox as box
def main_menu():
window = Tk()
window.title('Juke Box')
window.geometry('800x480')
window.configure(background = 'black')
label = Label(window, text = 'Juke-Box', fg = 'light green',
bg = 'black', font = (None, 30), height = 2)
label.pack(side = TOP)
gridFrame = Frame(window, bg='black') # New frame to store buttons
# Grid uses sticky instead of anchor, but in this scenario it is not really necessary
# I have left it in case you need it for some other reason
Jam = Button(gridFrame, text = 'The Jam', width = 25, height = 2)
Jam.grid(row=0, column=0, pady = 10, padx = 25, sticky=W)
Roses = Button(gridFrame, text = 'The Stone Roses', width = 25, height = 2)
Roses.grid(row=1, column=0, pady = 10, padx = 25, sticky=W)
Smiths = Button(gridFrame, text = 'The Smiths', width = 25, height = 2)
Smiths.grid(row=2, column=0, pady = 10, padx = 25, sticky =W)
Wedding = Button(gridFrame, text = 'The Wedding Pressent', width = 25, height = 2)
Wedding.grid(row=3, column=0, pady = 10, padx = 25, sticky =W)
Blondie = Button(gridFrame, text = 'Blondie', width = 25, height = 2)
Blondie.grid(row=4, column=0, pady = 10, padx = 25, sticky =W)
Clash = Button(gridFrame, text = 'Clash', width = 25, height = 2)
Clash.grid(row=5, column=0, pady = 10, padx = 25, sticky =W)
Madness = Button(gridFrame, text = 'Madness', width = 25, height = 2)
Madness.grid(row=0, column=1, pady = 10, padx = 25, sticky =N)
Pistols = Button(gridFrame, text = 'The Sex Pistols', width = 25, height = 2)
Pistols.grid(row=1, column=1, pady = 10, padx = 25, sticky =N)
gridFrame.pack(side=BOTTOM)
# Place it a the bottom or top or wherever you want it to go
window.mainloop()
main_menu()这是一个屏幕截图,展示了它在网格几何中的样子:

这是你想要的吗?
希望它能帮助^^
https://stackoverflow.com/questions/35812894
复制相似问题