【发布时间】:2021-07-06 17:59:06
【问题描述】:
我的 GUI 应用程序有两个文件:gui.py 包含所有 Tkinter 对象,controller.py 包含逻辑。逻辑是一个主函数def automation():,它嵌套了几个其他函数。该应用程序非常简单,只有一个按钮调用automation()。
我想在 GUI 小部件中添加出现在终端中的打印语句和错误,以便用户可以看到发生了什么。我找不到为导入的模块执行此操作的方法。
gui.py
import tkinter as tk
from tkinter import *
from controller import automation
root = tk.Tk()
frame_button = tk.Frame(root)
button = Button(frame_button, text="Ship", command=lambda:automation())
lower_frame = tk.Frame(root)
terminal = tk.Label(lower_frame)
frame_button.place()
button.place()
lower_frame.place()
terminal.place()
controller.py
def automation():
def folder_cleaner():
print('Folders cleaned')
def dispatch():
print('Dispatch done')
def ship():
print('Shipment successful')
def process():
folder_cleaner()
dispatch()
ship()
process()
这非常简化,但每个函数都有许多不同类型的输出。如何将它们全部重定向到 gui.py 和 terminal widget 内?
【问题讨论】:
-
this:
command=lambda:automation()可以缩短为:command=automation
标签: python function user-interface tkinter