- 论坛徽章:
- 4
|
本帖最后由 ssfjhh 于 2013-01-22 16:17 编辑
http://zetcode.com/gui/tkinter/layout/
上面这个链接是一个tkinter教程,该页面上有个布局如下,我尝试着用pack方法写出这个布局,但是没成功,求高手给个用pack方法写成的布局。
![]()
上面这个布局可以用grid方法写出,代码如下:- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- """
- ZetCode Tkinter tutorial
- In this script, we use the grid
- manager to create a more complicated
- layout.
- author: Jan Bodnar
- last modified: December 2010
- website: www.zetcode.com
- """
- from Tkinter import Tk, Text, BOTH, W, N, E, S
- from ttk import Frame, Button, Label, Style
- class Example(Frame):
-
- def __init__(self, parent):
- Frame.__init__(self, parent)
-
- self.parent = parent
-
- self.initUI()
-
- def initUI(self):
-
- self.parent.title("Windows")
- self.style = Style()
- self.style.theme_use("default")
- self.pack(fill=BOTH, expand=1)
- self.columnconfigure(1, weight=1)
- self.columnconfigure(3, pad=7)
- self.rowconfigure(3, weight=1)
- self.rowconfigure(5, pad=7)
-
- lbl = Label(self, text="Windows")
- lbl.grid(sticky=W, pady=4, padx=5)
-
- area = Text(self)
- area.grid(row=1, column=0, columnspan=2, rowspan=4,
- padx=5, sticky=E+W+S+N)
-
- abtn = Button(self, text="Activate")
- abtn.grid(row=1, column=3)
- cbtn = Button(self, text="Close")
- cbtn.grid(row=2, column=3, pady=4)
-
- hbtn = Button(self, text="Help")
- hbtn.grid(row=5, column=0, padx=5)
- obtn = Button(self, text="OK")
- obtn.grid(row=5, column=3)
-
- def main():
-
- root = Tk()
- root.geometry("350x300+300+300")
- app = Example(root)
- root.mainloop()
- if __name__ == '__main__':
- main()
复制代码 另外我用grid方法写了个布局,如下图所示,一个窗口中包含一个Text控件和两个Button控件,两个按钮位于Text控件的下方的正中央,可以用pack方法实现吗?
我自己试了几次也没有实现,使用lbtn.pack(side = BOTTOM),rbtn.pack(side = BOTTOM)这串代码,两个按钮均在正中央,但是不在同一行;
分别使用lbtn.pack(side = LEFT), rbtn.pack(side= RIGHT)时,两个按钮又分别在Text控件的下方的两侧;
使用lbtn.pack(side = LEFT),rbtn.pack(side = LEFT)时,两个按钮又同时Text控件的下方的左侧;
使用lbtn.pack(side = RIGHT),rbtn.pack(side = RIGHT)时,两个按钮又同时Text控件的下方的右侧;
有办法用pack方法实现我用grid方法实现的布局吗?
我用grid方法实现的布局代码如下:- from tkinter import *
- from tkinter.ttk import *
- root = Tk()
- t = Text(root)
- root.columnconfigure(0,weight = 1)
- root.columnconfigure(1, weight = 1)
- root.rowconfigure(0, weight = 1)
- t.grid(row = 0, column = 0, rowspan = 2, columnspan = 2, sticky = E+W+S+N)
- lbtn = Button(root, text = 'left')
- rbtn = Button(root, text = 'right')
- lbtn.grid(row = 2, column = 0, sticky = SE)
- rbtn.grid(row = 2, column = 1, sticky = SW)
- root.mainloop(
复制代码 |
|