Python Tkinter,使用pack:can';不要将页脚栏保持在底部 我试图让一行三个按钮均匀间隔,但因为我使用的是边=“左”和边=“右”< /代码>,其他按钮在中间结束。
示例代码: 将tkinter作为tki导入Python Tkinter,使用pack:can';不要将页脚栏保持在底部 我试图让一行三个按钮均匀间隔,但因为我使用的是边=“左”和边=“右”< /代码>,其他按钮在中间结束。,python,layout,tkinter,Python,Layout,Tkinter,示例代码: 将tkinter作为tki导入 class App(object): def __init__(self): self.root = tki.Tk() self.root.config(bg="white") self.root.geometry("400x300") top_frm = tki.Frame(self.root).pack() T = tki.Text(top_frm, h
class App(object):
def __init__(self):
self.root = tki.Tk()
self.root.config(bg="white")
self.root.geometry("400x300")
top_frm = tki.Frame(self.root).pack()
T = tki.Text(top_frm, height=2, width=30)
T.pack()
T.insert(tki.END, "Just a text Widget\nin two lines\n")
mdl_frm = tki.Frame(self.root, width="400").pack(fill="both",expand=True)
lbut = tki.Button(mdl_frm, text='Left button').pack(side="left")
rbut = tki.Button(mdl_frm, text='right button').pack(side="right")
bottom_frm = tki.Frame(self.root).pack(side="bottom",fill="x",expand=False)
btn_frm_r = tki.Frame(bottom_frm).pack(side="right",fill="x")
btn_frm_c = tki.Frame(bottom_frm).pack(side="right",fill="x")
btn_frm_l = tki.Frame(bottom_frm).pack(side="right",fill="x")
button1 = tki.Button(btn_frm_r,text='Bottom button 1').pack()
button2 = tki.Button(btn_frm_c,text='Bottom button 2').pack()
button3 = tki.Button(btn_frm_l,text='Bottom button 3').pack()
app = App()
#launch the app
app.root.mainloop()
我试着把“左键”和“右键”放在一个框架里。
我在某个地方读到,当使用side
声明时,当给定某个区域时,它会完全控制该区域,因此我尝试先声明btm\u frm
帧,然后使用side=“bottom”
有什么想法吗?.pack()
返回None
,因此不能.pack()
并在一行上赋值给变量。此外,我建议在您将其他物品放入框架后将其打包。
最后,我建议对类属性使用
self.
;当您以后需要访问它们时,您会发现它很有用
import tkinter as tki
class App(object):
def __init__(self):
self.root = tki.Tk()
self.root.config(bg="white")
self.root.geometry("400x300")
self.top_frm = tki.Frame(self.root)
self.top_frm.pack()
self.T = tki.Text(self.top_frm, height=2, width=30)
self.T.pack()
self.T.insert(tki.END, "Just a text Widget\nin two lines\n")
self.mdl_frm = tki.Frame(self.root, width="400")
self.mdl_frm.pack(fill="both",expand=True)
self.lbut = tki.Button(self.mdl_frm, text='Left button')
self.lbut.pack(side="left")
self.rbut = tki.Button(self.mdl_frm, text='right button')
self.rbut.pack(side="right")
self.bottom_frm = tki.Frame(self.root)
self.btn_frm_r = tki.Frame(self.bottom_frm)
self.btn_frm_r.pack(side="right",fill="x")
self.btn_frm_c = tki.Frame(self.bottom_frm)
self.btn_frm_c.pack(side="right",fill="x")
self.btn_frm_l = tki.Frame(self.bottom_frm)
self.btn_frm_l.pack(side="right",fill="x")
self.button1 = tki.Button(self.btn_frm_r,text='Bottom button 1')
self.button1.pack()
self.button2 = tki.Button(self.btn_frm_c,text='Bottom button 2')
self.button2.pack()
self.button3 = tki.Button(self.btn_frm_l,text='Bottom button 3')
self.button3.pack()
self.bottom_frm.pack(side="bottom",fill="x",expand=False)
app = App()
#launch the app
app.root.mainloop()
.pack()
返回None
,因此不能.pack()
并在一行上赋值给变量。此外,我建议在您将其他物品放入框架后将其打包。
最后,我建议对类属性使用
self.
;当您以后需要访问它们时,您会发现它很有用
import tkinter as tki
class App(object):
def __init__(self):
self.root = tki.Tk()
self.root.config(bg="white")
self.root.geometry("400x300")
self.top_frm = tki.Frame(self.root)
self.top_frm.pack()
self.T = tki.Text(self.top_frm, height=2, width=30)
self.T.pack()
self.T.insert(tki.END, "Just a text Widget\nin two lines\n")
self.mdl_frm = tki.Frame(self.root, width="400")
self.mdl_frm.pack(fill="both",expand=True)
self.lbut = tki.Button(self.mdl_frm, text='Left button')
self.lbut.pack(side="left")
self.rbut = tki.Button(self.mdl_frm, text='right button')
self.rbut.pack(side="right")
self.bottom_frm = tki.Frame(self.root)
self.btn_frm_r = tki.Frame(self.bottom_frm)
self.btn_frm_r.pack(side="right",fill="x")
self.btn_frm_c = tki.Frame(self.bottom_frm)
self.btn_frm_c.pack(side="right",fill="x")
self.btn_frm_l = tki.Frame(self.bottom_frm)
self.btn_frm_l.pack(side="right",fill="x")
self.button1 = tki.Button(self.btn_frm_r,text='Bottom button 1')
self.button1.pack()
self.button2 = tki.Button(self.btn_frm_c,text='Bottom button 2')
self.button2.pack()
self.button3 = tki.Button(self.btn_frm_l,text='Bottom button 3')
self.button3.pack()
self.bottom_frm.pack(side="bottom",fill="x",expand=False)
app = App()
#launch the app
app.root.mainloop()
对按钮使用
side='left'
或side='right'
,因为您希望它们在其包含的框架内从左向右移动
button1 = tki.Button(btn_frm_r,text='Bottom button 1').pack(side="left")
button2 = tki.Button(btn_frm_c,text='Bottom button 2').pack(side="left")
button3 = tki.Button(btn_frm_l,text='Bottom button 3').pack(side="left")
顺便说一下,如果您要执行tki.Button(…).pack(…)
,那么将其指定给变量是没有意义的。变量将始终具有值None
pack
算法的完整解释如下:对按钮使用side='left'
或side='right'
,因为您希望它们在其包含的框架内从左向右移动
button1 = tki.Button(btn_frm_r,text='Bottom button 1').pack(side="left")
button2 = tki.Button(btn_frm_c,text='Bottom button 2').pack(side="left")
button3 = tki.Button(btn_frm_l,text='Bottom button 3').pack(side="left")
顺便说一下,如果您要执行tki.Button(…).pack(…)
,那么将其指定给变量是没有意义的。变量将始终具有值None
pack
算法的完整解释如下: