如何在tkinter的输出框中输出纯文本

时间:2017-12-01 20:40:55

标签: python tkinter

我写了"指令"对于.txt文件中的tkinter程序,当按下“指令”按钮时,我想创建一个单独的窗口,将指令从文本文件复制到输出框中。

如果有人能帮助我,我将非常感激。 这是我到目前为止所做的,但它根本不起作用。

Integer.parseInt("0x11", 16));

1 个答案:

答案 0 :(得分:1)

当我想要第二个窗口时,message box通常可以做到这一点,如下所示:

from Tkinter import *
from tkMessageBox import showinfo

class App:

    def __init__(self, master):

        frame = Frame(master)
        frame.pack()

        self.button = Button(frame, text="QUIT", fg="red", command=frame.quit)
        self.button.pack(side=LEFT)

        self.instr = Button(frame, text="Instruction", command=self.instruction)
        self.instr.pack(side=LEFT)

    def instruction(self):
        try:
            with open("instruction.txt") as fp:
                message = fp.read()
        except IOError:
            message = 'No instructions available'
        showinfo("Instructions", message)

root = Tk()
app = App(root)
root.mainloop()

或者,如果您更喜欢OOP style

# template: https://stackoverflow.com/a/17470842/8747

# Use Tkinter for python 2, tkinter for python 3
import Tkinter as tk
import tkMessageBox


class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        self.button = tk.Button(
            self, text="QUIT", fg="red", command=self.quit)

        self.instr = tk.Button(
            self, text="Instruction", command=self.instruction)

        self.button.pack(side=tk.LEFT)
        self.instr.pack(side=tk.LEFT)

    def instruction(self):
        try:
            with open("instruction.txt") as fp:
                message = fp.read()
        except IOError:
            message = 'No instruction available'

        msg = tkMessageBox.showinfo("Instruction", message)

if __name__ == "__main__":
    root = tk.Tk() 
    MainApplication(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

当然,有时消息框不够灵活,您需要创建一个top-level窗口:

# template: https://stackoverflow.com/a/17470842/8747

# Use Tkinter for python 2, tkinter for python 3
import Tkinter as tk
import tkMessageBox


class Instruction(tk.Toplevel):
    def __init__(self, parent, message, *args, **kwargs):
        tk.Toplevel.__init__(self, parent, *args, **kwargs)

        self.msg = tk.Message(self, text=message)
        self.button = tk.Button(self, text="Dismiss", command=self.destroy)

        self.msg.pack()
        self.button.pack()

class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        self.button = tk.Button(
            self, text="QUIT", fg="red", command=self.quit)

        self.instr = tk.Button(
            self, text="Instruction", command=self.instruction)

        self.button.pack(side=tk.LEFT)
        self.instr.pack(side=tk.LEFT)

    def instruction(self):
        try:
            with open("instruction.txt") as fp:
                message = fp.read()
        except IOError:
            message = 'No instruction available'

        msg = Instruction(self, message)

if __name__ == "__main__":
    root = tk.Tk()
    MainApplication(root).pack(side="top", fill="both", expand=True)
    root.mainloop()