【问题标题】:How do I position buttons in Tkinter?如何在 Tkinter 中定位按钮?
【发布时间】:2017-01-10 16:57:08
【问题描述】:

我这里有一个程序,里面有两个按钮。我正在尝试将它们的位置更改为它们之间的空间,因为它们目前位于彼此的正下方。我应该怎么做才能改变按钮的位置?

def menu():
    import tkinter
    window=tkinter.Tk()
    window.title('Lightning Parties')
    window.configure(background='turquoise')
    lbl=tkinter.Label(window, text='Welcome to Lightning Parties!', fg='purple', bg='turquoise', font=('comicsans', 14))
    lbl.pack()

    #here are the buttons
    lbl=tkinter.Button(window, text='Returning customer options', fg='white',   bg='purple', font=('comicsans', 12),command=combine_funcs(window.destroy, customer_login))
    lbl.pack()

    lbl=tkinter.Button(window, text='Register as a customer', fg='white', bg='purple', font=('comicsans', 12),command=combine_funcs(window.destroy, customer_details))
    lbl.pack()

任何帮助将不胜感激!

【问题讨论】:

  • pack 命令接受一个选项来控制填充。你试过用那个吗?

标签: python button tkinter position


【解决方案1】:

其实上学期我也做了一些Tkinter的应用,就是老师给我们的项目。所以我去了一些 Python 的教程网站,找到了三种将小部件放置在输出屏幕上的方法。这三种方法是

 1. Pack()
 2. Grid()
 3. Place() #the best method over Pack() and Grid()

Place() 方法以 x 和 y 的形式获取坐标。请参阅此链接以获取更多说明https://www.tutorialspoint.com/python/python_gui_programming.htm

https://www.tutorialspoint.com/python/tk_place.htm

查看给定链接页面的底部。Place() 方法是用其正确的参数定义的。我会更喜欢 Place() 方法而不是 Pack() 和 Grid(),因为它像我们在 html 中使用的 CSS 一样工作,因为它采用 (width,height) 的值并根据您想要的位置放置您的小部件。强>

如果您找到答案,我们将不胜感激。

【讨论】:

  • 不确定我是否同意“地点”是最好的。当您无法让 grid 或 pack 做您想做的事时,这更像是最后的手段! CSS 有许多网格布局系统,对许多网站都非常有用。使用 place 意味着您必须自己进行所有屏幕尺寸、小部件尺寸、尺寸更改计算。网格系统的灵活性(特别是如果您将框架嵌套在框架内)可能会减少工作量并提高响应速度。我为它构建的一个应用程序有一对并排的框架打包在一起,每个框架都包含一个单独的网格。
【解决方案2】:

我的是这样的:

object1 = Tk()
actionBtn = Button(object1, text="Enter", width=15, height=2, command=quit).place(x=0, y=0)

#.place() is the best thing to use, x and y determine the location in terms of geometry.

您甚至可以添加带有 .png 扩展名的图片,如下所示:

buttonEnter = PhotoImage(file="buttonEnter.png") #image file must be inserted 
buttonEnter1 = Button(object1, image=buttonEnter, width=20, height=4).place(x=0, y=0) 

【讨论】:

    【解决方案3】:

    要增加按钮之间的垂直间距,请在pack 方法中使用pady 参数:lbl.pack(pady=10)。

    对于水平间距,有一个padx 参数。这也适用于 grid 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-19
      • 2020-07-19
      • 1970-01-01
      • 2022-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多