【问题标题】:Python tkinter: Make the output appear in a text box on GUI not in the shellPython tkinter:使输出出现在 GUI 上的文本框中,而不是在 shell 中
【发布时间】:2013-11-30 15:47:11
【问题描述】:

我有一个 tkinter 菜单,可以让用户选择一些功能。我想如果该函数的输出显示在 Tkinter 的 texbox 上而不是 Shell 上。我对 Tkinter 非常陌生。我不知道该怎么做..

有些功能需要用户输入,用户可以直接从Tkinter Gui输入吗?

程序菜单代码:

函数:situacaocorrente()、findfuncionario()、verhistorico()这里没有介绍。

代码

from Tkinter import Tk, Frame, Menu, Label, Text
import sys



class Example(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)   

        self.parent = parent
        self.output = Text(self)
        self.output.pack()
        sys.stdout = self
        self.initUI()

    def initUI(self):

        self.parent.title("High Flex")


        menubar = Menu(self.parent)
        self.parent.config(menu=menubar)

        fileMenu = Menu(menubar)

        fileMenu.add_command(label="Procurar funcionario", command=self.procurarfuncionario)
        fileMenu.add_command(label="Ver Historico", command=self.historico)
        fileMenu.add_command(label="Verificar Onde se encontram os funcionarios", command=self.funcionariosturno)

        fileMenu.add_command(label="Sair", command=self.onExit)
        menubar.add_cascade(label="INICIAR", menu=fileMenu)

    def funcionariosturno(self):
        situacaocorrente()

    def procurarfuncionario(self):

        findfuncionario()

    def historico (self):
        verhistorico()


    def onExit(self):
        self.quit()

    def write(self, txt):
        self.output.insert(END,str(txt))




def main():

    root = Tk()
    root.geometry("250x150+300+300")
    app = Example(root)


    root.mainloop()

【问题讨论】:

  • 我建议你看看EasyGUI。它可能有你需要的东西,即使没有,它也会为你提供一些使用 Tkinter 的好例子。
  • 仅供参考... EasyGUI 在今年早些时候被正式宣布死亡。不过,它可能仍然有用。

标签: python textbox tkinter output


【解决方案1】:

您可以使用Entry() 获取用户输入。查看更多信息:An Introduction to Tkinter

我不知道您尝试在 self.output 中输入什么函数输出 - 来自 shell 中的外部程序或 python 函数。

sys.stdout = self 仅适用于 Python 函数,例如 print

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    • 2019-02-03
    • 2016-09-06
    • 1970-01-01
    相关资源
    最近更新 更多