鱼C论坛

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

[Tkinter] Tkinter 布局管理器:pack

[复制链接]
发表于 2015-5-25 16:50:36 | 显示全部楼层 |阅读模式
购买主题 已有 12 人购买  本主题需向作者支付 10 鱼币 才能浏览
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

更新的~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

谢谢哥们长期支持~
小甲鱼最新课程 -> https://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()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-26 04:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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