免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1896 | 回复: 0
打印 上一主题 下一主题

用grid方法写出的布局都可以用pack方法写出吗? [复制链接]

论坛徽章:
4
金牛座
日期:2013-10-11 16:12:50卯兔
日期:2014-07-31 09:17:19辰龙
日期:2014-08-08 09:28:02狮子座
日期:2014-09-14 20:32:05
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-01-22 16:11 |只看该作者 |倒序浏览
本帖最后由 ssfjhh 于 2013-01-22 16:17 编辑

http://zetcode.com/gui/tkinter/layout/
上面这个链接是一个tkinter教程,该页面上有个布局如下,我尝试着用pack方法写出这个布局,但是没成功,求高手给个用pack方法写成的布局。



上面这个布局可以用grid方法写出,代码如下:
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-

  3. """
  4. ZetCode Tkinter tutorial

  5. In this script, we use the grid
  6. manager to create a more complicated
  7. layout.

  8. author: Jan Bodnar
  9. last modified: December 2010
  10. website: www.zetcode.com
  11. """

  12. from Tkinter import Tk, Text, BOTH, W, N, E, S
  13. from ttk import Frame, Button, Label, Style


  14. class Example(Frame):
  15.   
  16.     def __init__(self, parent):
  17.         Frame.__init__(self, parent)   
  18.          
  19.         self.parent = parent
  20.         
  21.         self.initUI()
  22.         
  23.     def initUI(self):
  24.       
  25.         self.parent.title("Windows")
  26.         self.style = Style()
  27.         self.style.theme_use("default")
  28.         self.pack(fill=BOTH, expand=1)

  29.         self.columnconfigure(1, weight=1)
  30.         self.columnconfigure(3, pad=7)
  31.         self.rowconfigure(3, weight=1)
  32.         self.rowconfigure(5, pad=7)
  33.         
  34.         lbl = Label(self, text="Windows")
  35.         lbl.grid(sticky=W, pady=4, padx=5)
  36.         
  37.         area = Text(self)
  38.         area.grid(row=1, column=0, columnspan=2, rowspan=4,
  39.             padx=5, sticky=E+W+S+N)
  40.         
  41.         abtn = Button(self, text="Activate")
  42.         abtn.grid(row=1, column=3)

  43.         cbtn = Button(self, text="Close")
  44.         cbtn.grid(row=2, column=3, pady=4)
  45.         
  46.         hbtn = Button(self, text="Help")
  47.         hbtn.grid(row=5, column=0, padx=5)

  48.         obtn = Button(self, text="OK")
  49.         obtn.grid(row=5, column=3)        
  50.               

  51. def main():
  52.   
  53.     root = Tk()
  54.     root.geometry("350x300+300+300")
  55.     app = Example(root)
  56.     root.mainloop()  


  57. if __name__ == '__main__':
  58.     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方法实现的布局代码如下:
  1. from tkinter import *
  2. from tkinter.ttk import *
  3. root = Tk()
  4. t = Text(root)
  5. root.columnconfigure(0,weight = 1)
  6. root.columnconfigure(1, weight = 1)
  7. root.rowconfigure(0, weight = 1)
  8. t.grid(row = 0, column = 0, rowspan = 2, columnspan = 2, sticky = E+W+S+N)
  9. lbtn = Button(root, text = 'left')
  10. rbtn = Button(root, text = 'right')
  11. lbtn.grid(row = 2, column = 0, sticky = SE)
  12. rbtn.grid(row = 2, column = 1, sticky = SW)
  13. root.mainloop(
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP