我如何使Tkinker小部件忽略小部件上方行中小部件的宽度(仅浮动到左侧)?

时间:2018-08-16 00:15:16

标签: python-2.7 tkinter tkinter-layout

我正在尝试创建一种计算器类型的布局,但是当我添加输出字段时,数字混乱了。

import Tkinter as tk

window = tk.Tk()
window.title("BUTTON CALCULATOR")
window.geometry("400x400")


#----OutPut----
answer_output = tk.Text(master = window, height = 1, width = 20)
answer_output.grid(column = 0, row = 0)

#----BUTTONS----

buttonOne = tk.Button(text = "1")
buttonOne.grid(column = 0, row = 1)

numberTwo = tk.Button(text = "2")
numberTwo.grid(column = 1, row = 1)

numberThree = tk.Button(text = "3")
numberThree.grid(column = 2, row = 1)

window.mainloop()

我如何使1、2和3在窗口的左宽处彼此相邻?

enter image description here

1 个答案:

答案 0 :(得分:2)

主要问题是文本框缺少columnspan。接下来,您可能需要的是按钮上的sticky,以便它们在列中伸展。

看看下面的例子,如果您有任何疑问,请告诉我。

import tkinter as tk

window = tk.Tk()
window.title("BUTTON CALCULATOR")
window.geometry("400x400")

answer_output = tk.Text(master=window, height=1, width=20)
answer_output.grid(column=0, row=0, columnspan=3)

buttonOne = tk.Button(text="1")
buttonOne.grid(column=0, row=1, sticky="ew")

numberTwo = tk.Button(text="2")
numberTwo.grid(column=1, row=1, sticky="ew")

numberThree = tk.Button(text="3")
numberThree.grid(column=2, row=1, sticky="ew")

window.mainloop()

结果:

enter image description here