【问题标题】:How to place buttons in tkinter side by side and fill Y at the top of the widget如何在 tkinter 中并排放置按钮并在小部件顶部填充 Y
【发布时间】:2021-06-17 18:07:20
【问题描述】:

我正在尝试将小部件顶部的三个按钮并排对齐并填充“Y”和“X”,而不更改它们正下方的文本小部件的位置。我尝试使用这些 anchor=NW,N,NE 将按钮并排放置,但它不起作用我不知道如何用英语说,所以这是我得到的图像:

这是我的代码:

root = Tk()        
                    


screen_x = int(root.winfo_screenwidth())
screen_y = int(root.winfo_screenheight()) - int(root.winfo_screenheight()) * int(9.1145833333) // 100



window_x = 512
window_y = 690 

posX = (screen_x // 2) - (window_x // 2)
posY = (screen_y // 2) - (window_y // 2)

geo = "{}x{}+{}+{}". format(window_x, window_y, posX, posY)

root.geometry(geo)
root.resizable(0,0)
root.title("StoryApp")
root.config(background="gray8")

photo2 = PhotoImage(file = "Newico.png")
root.iconphoto(False, photo2)

Btn1 = Button(root, text="Button1", command=passit, padx=5, pady=5,borderwidth=5, relief="sunken", activebackground="dark red", bg="gray15", fg="red")
Btn1.configure(height=1, width=6)
Btn1.pack(padx=10, pady=5, side=TOP, anchor=NW, fill=Y)




Btn2 = Button(root, text="Button2", command=passit, padx=2, pady=5,borderwidth=5, relief="sunken", activebackground="dark red", bg="gray15", fg="red")
Btn2.configure(height=1, width=6)
Btn2.pack(padx=10, pady=5,side=TOP, anchor=N, fill=Y)

Btn3 = Button(root, text="Button3", command=passit, padx=2, pady=5,borderwidth=5, relief="sunken", activebackground="dark red", bg="gray15", fg="red")
Btn3.configure(height=1, width=6)
Btn3.pack(padx=10, pady=5,side=TOP, anchor=NE, fill=Y)





DecodedTextBoxTitle = LabelFrame(root, text="Decoded code", padx=5, pady=5, height=260)
DecodedTextBoxTitle.config(background="gray8")
DecodedTextBoxTitle.config(foreground="red")
DecodedTextBoxTitle.pack(padx=10, pady=5, fill="both", expand=True)

DecodedTextBox = Text(DecodedTextBoxTitle,wrap='none', undo=True, autoseparators=True,borderwidth=5, relief="sunken",selectbackground="dark red")
DecodedTextBox.config(highlightbackground="gray15")
DecodedTextBox.config(foreground="red")
DecodedTextBox.config(highlightthickness=3)
DecodedTextBox.config(highlightcolor="dark red")
DecodedTextBox.config(background="gray15")
DecodedTextBox.pack(fill="both", expand=True)
DecodedTextBox.configure(state='normal')

【问题讨论】:

  • side='left' 或“右”添加到按钮的.pack() 方法或使用.grid(您需要为此打包一个框架并将这些小部件放入其中)跨度>

标签: python-3.x tkinter tkinter-button


【解决方案1】:

如果你想使用pack,那么你应该为按钮创建一个框架。然后,您可以使用pack 将框架放在上面,将 LabelFrame 放在下面。

或者,您可以切换到使用grid,这样您就可以将每个按钮放在同一行但不同的列中。

【讨论】:

  • 在网格中,列太长,而且我希望按钮填充它们所在的位置,就像填充它们所在的列一样。这可能吗?
  • @ShadowKurgansk:是的,这是可能的。列可以任意高或短。
  • 抱歉回答晚了,我刚有时间。我们如何改变更长的时间?在我所做的研究中,默认更长的时间已经是最短的时间了,这并不像我想要的那样短。
猜你喜欢
  • 1970-01-01
  • 2013-05-09
  • 1970-01-01
  • 1970-01-01
  • 2022-11-24
  • 1970-01-01
  • 1970-01-01
  • 2015-05-06
  • 2021-12-23
相关资源
最近更新 更多