Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Tkinter中定位按钮?_Python_Button_Tkinter_Position - Fatal编程技术网

Python 如何在Tkinter中定位按钮?

Python 如何在Tkinter中定位按钮?,python,button,tkinter,position,Python,Button,Tkinter,Position,我这里有一个程序,里面有两个按钮。我正试图改变它们的位置,使之成为它们之间的一个空间,因为目前它们正处于彼此的正下方。我应该如何更改按钮的位置 def menu(): import tkinter window=tkinter.Tk() window.title('Lightning Parties') window.configure(background='turquoise') lbl=tkinter.Label(window, text='Welc

我这里有一个程序,里面有两个按钮。我正试图改变它们的位置,使之成为它们之间的一个空间,因为目前它们正处于彼此的正下方。我应该如何更改按钮的位置

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
方法中使用
pady
参数:
lbl.pack(pady=10)


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

我会对其他答案发表评论,但没有代表。
建议使用
pad
方法的答案可能不是您想要的
pad
在按钮边框和内容之间创建了额外的空间,因此使用它只会使按钮本身变大。

我可能错了,但我不认为这会影响小部件之间的间距。

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

 1. Pack()
 2. Grid()
 3. Place() #the best method over Pack() and Grid()
Place()方法以x和y的形式获取坐标。有关更多说明,请参阅此链接

查看给定链接的页面底部。Place()方法是用其正确的参数定义的。与Pack()和Grid()相比,我更喜欢Place()方法,因为它的工作原理与我们在html中使用的CSS类似,因为它接受(宽度、高度)的值,并根据需要将小部件放置在任何位置。

如果你找到了答案,请竖起大拇指。我的大拇指是这样的:

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) 

pack命令接受控制填充的选项。你试过使用它吗?用
pack(pady=10)
而不是
pack()
试试问题中的代码示例,你会发现它增加了按钮之间的垂直间距。填充并不会使按钮本身变大。是的,谢谢,我把小部件中的pad关键字和网格中的pad关键字搞混了()不确定我是否同意“地点”是最好的。当你不能让网格或包做你想做的事情时,它更像是最后的手段!CSS有很多网格布局系统,对很多网站都很有用。使用place意味着您必须自己进行所有屏幕大小、小部件大小和大小更改计算。网格系统的灵活性(特别是如果将框架嵌套在框架中)可能会减少工作量并提高响应速度。我为一个应用程序设计了一对并排的框架,每个框架都包含一个单独的网格。