在 Tkinter 中使用锚点 [英] Using Anchor In Tkinter

查看:55
本文介绍了在 Tkinter 中使用锚点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对自动点唱机进行编程,但我处于早期阶段,在使用 anchor 时遇到了问题.

I am trying to program a juke box however I am at early stages and I have come across a problem when using 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' 但是它把它们全部移到底部.

My problem is because I have ran out of room when stacking my albums down the left I want to start stacking them down the middle so I used anchor = 'n' however it moved them all to the bottom.

请有人帮我找到一种方法,从顶部中间开始堆叠我的相册.

Please could someone help me find a way to start stacking my albums from the top middle.

我使用的是 python 3.

I am using python 3.

推荐答案

对于你想做的事情,grid 几何似乎是一个更好的主意,你可以明确地编码在哪里item 将而不是让 pack 几何体隐含地决定它的去向.另一个改进是有一个 Frame,您可以在其中放置每个专辑 Button.我已经编辑了你的一段代码:

For the kind of thing you want to do, the grid geometry seems like a much better idea, you can explicitly code where the item will be rather than let the pack geometry implicitly determine where it will go. Another improvement, would be to have a Frame where you put each album Button. I have edited your piece of code:

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

这是网格几何图形的屏幕截图:

Here is a screenshot of how it looks like with the grid geometry:

这是你想要的吗?

希望对你有帮助^^

这篇关于在 Tkinter 中使用锚点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆