鱼C论坛

 找回密码
 立即注册
查看: 17053|回复: 14

[Tkinter] Tkinter 布局管理器:pack

[复制链接]
发表于 2015-5-25 16:50:36 | 显示全部楼层 |阅读模式
购买主题 已有 12 人购买  本主题需向作者支付 10 鱼币 才能浏览
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-5-25 18:42:36 | 显示全部楼层
Python还在更新吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-5-25 21:27:31 | 显示全部楼层
小甲鱼的PYTHON既全面又权威!太感谢了!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-5-25 22:07:58 | 显示全部楼层
萌萌哒 发表于 2015-5-25 18:42
Python还在更新吗?

更新的~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-5-25 22:08:06 | 显示全部楼层
delphi369 发表于 2015-5-25 21:27
小甲鱼的PYTHON既全面又权威!太感谢了!

谢谢哥们长期支持~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-5-26 10:05:39 | 显示全部楼层
本帖最后由 我是桃川人 于 2015-5-26 10:07 编辑

这个说明早出就更好了~看官方英文理解不够。
赞~
请教一个问题,如何在tkinter里处理while?bind吗?怎么bind一个TCP监听呢?如:
  1. client = sock(AF_INET, SOCK_STREAM)
  2. client.connect((HOST, PORT))
  3. while True:
  4.     data = clent.recv(1024)
  5.     ...
复制代码



想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-5-26 18:22:48 | 显示全部楼层
Python3 将以下代码的 Tkinter 替换成 tkinter 即可:

  1. """
  2. This recipe describes how to handle asynchronous I/O in an environment where
  3. you are running Tkinter as the graphical user interface. Tkinter is safe
  4. to use as long as all the graphics commands are handled in a single thread.
  5. Since it is more efficient to make I/O channels to block and wait for something
  6. to happen rather than poll at regular intervals, we want I/O to be handled
  7. in separate threads. These can communicate in a threasafe way with the main,
  8. GUI-oriented process through one or several queues. In this solution the GUI
  9. still has to make a poll at a reasonable interval, to check if there is
  10. something in the queue that needs processing. Other solutions are possible,
  11. but they add a lot of complexity to the application.

  12. Created by Jacob Hallén, AB Strakt, Sweden. 2001-10-17
  13. """
  14. import Tkinter
  15. import time
  16. import threading
  17. import random
  18. import Queue

  19. class GuiPart:
  20.     def __init__(self, master, queue, endCommand):
  21.         self.queue = queue
  22.         # Set up the GUI
  23.         console = Tkinter.Button(master, text='Done', command=endCommand)
  24.         console.pack()
  25.         # Add more GUI stuff here

  26.     def processIncoming(self):
  27.         """
  28.         Handle all the messages currently in the queue (if any).
  29.         """
  30.         while self.queue.qsize():
  31.             try:
  32.                 msg = self.queue.get(0)
  33.                 # Check contents of message and do what it says
  34.                 # As a test, we simply print it
  35.                 print msg
  36.             except Queue.Empty:
  37.                 pass

  38. class ThreadedClient:
  39.     """
  40.     Launch the main part of the GUI and the worker thread. periodicCall and
  41.     endApplication could reside in the GUI part, but putting them here
  42.     means that you have all the thread controls in a single place.
  43.     """
  44.     def __init__(self, master):
  45.         """
  46.         Start the GUI and the asynchronous threads. We are in the main
  47.         (original) thread of the application, which will later be used by
  48.         the GUI. We spawn a new thread for the worker.
  49.         """
  50.         self.master = master

  51.         # Create the queue
  52.         self.queue = Queue.Queue()

  53.         # Set up the GUI part
  54.         self.gui = GuiPart(master, self.queue, self.endApplication)

  55.         # Set up the thread to do asynchronous I/O
  56.         # More can be made if necessary
  57.         self.running = 1
  58.             self.thread1 = threading.Thread(target=self.workerThread1)
  59.         self.thread1.start()

  60.         # Start the periodic call in the GUI to check if the queue contains
  61.         # anything
  62.         self.periodicCall()

  63.     def periodicCall(self):
  64.         """
  65.         Check every 100 ms if there is something new in the queue.
  66.         """
  67.         self.gui.processIncoming()
  68.         if not self.running:
  69.             # This is the brutal stop of the system. You may want to do
  70.             # some cleanup before actually shutting it down.
  71.             import sys
  72.             sys.exit(1)
  73.         self.master.after(100, self.periodicCall)

  74.     def workerThread1(self):
  75.         """
  76.         This is where we handle the asynchronous I/O. For example, it may be
  77.         a 'select()'.
  78.         One important thing to remember is that the thread has to yield
  79.         control.
  80.         """
  81.         while self.running:
  82.             # To simulate asynchronous I/O, we create a random number at
  83.             # random intervals. Replace the following 2 lines with the real
  84.             # thing.
  85.             time.sleep(rand.random() * 0.3)
  86.             msg = rand.random()
  87.             self.queue.put(msg)

  88.     def endApplication(self):
  89.         self.running = 0

  90. rand = random.Random()
  91. root = Tkinter.Tk()

  92. client = ThreadedClient(root)
  93. root.mainloop()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2015-9-18 10:54:09 | 显示全部楼层
小甲鱼 发表于 2015-5-25 22:08
谢谢哥们长期支持~

不用谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-9-5 19:09:01 | 显示全部楼层
anchor和side。。。expand和fill。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-24 22:08:17 | 显示全部楼层
谢谢分享啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-5-23 19:53:11 | 显示全部楼层
请问应该怎样做到左侧页面导航图?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

头像被屏蔽
发表于 2018-10-19 18:54:28 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-15 08:09:06 | 显示全部楼层

谢谢分享啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-31 21:47:32 | 显示全部楼层
思考了半天,自己的理解加上实践代码的结果展示,终于明白了anchor与side的区别
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-29 10:59:37 | 显示全部楼层
是不是fill=X不需要expand=True就可以默认填充,而其他比如fill=BOTH,fill=Y就需要expand=Ture来填充剩余位置?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-17 04:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表