Python tkinter文本小部件无法显示在所需窗口上

Python tkinter文本小部件无法显示在所需窗口上,python,linux,tkinter,Python,Linux,Tkinter,因此,我编写了一个简单的tkinter gui,当出现提示时会显示一些命令输出结果,唯一的问题是命令不会出现在任何其他屏幕上 def updateipt(): text = Text(main_window, height=150, width=124) text.pack() with Popen(["iptables", '-L', '-v', '-n'], stdout=PIPE, stderr=PIPE) as p: for l

因此,我编写了一个简单的tkinter gui,当出现提示时会显示一些命令输出结果,唯一的问题是命令不会出现在任何其他屏幕上

def updateipt():
    text = Text(main_window, height=150, width=124)
    text.pack()
    with Popen(["iptables", '-L', '-v', '-n'], stdout=PIPE, stderr=PIPE) as p:
        for line in p.stdout:
            text.insert(END, line,)
文本=文本(主窗口,高度=150,宽度=124)

我假设用另一个窗口替换上面的main_window参数会将文本小部件定向到另一个窗口,但每次尝试此操作时,我都会收到一个错误,指出该窗口需要是Tk():AttributeError:'function'对象没有属性'Tk'

我如何解决这个问题

完整代码如下:

#!/bin/bash
import os
import subprocess as sub
from tkinter import *
from subprocess import Popen, PIPE


def main_window():
    global main_window
    main_window = Tk()
    main_window.title("Firewall test")
    main_window.geometry("1000x700")
    main_window.configure(bg="dark grey")
    label1 = Label(main_window, text="What Would you like to configure?", font=('arial', 12, 'bold'), bg="white",
                   fg="black")
    label1.pack(fill=X, pady=20)

    btnproc = Button(main_window, text="Process configuration", font=('arial', 9, 'bold'), bg="light grey", fg="black",
                     width="30", height="2", command=lambda: process_window())
    btnproc.pack(pady=20)
    btnfw = Button(main_window, text="Firewall", font=('arial', 9, 'bold'), bg="light grey", fg="black", width="30",
                   height="2", command=lambda: firewall_window())
    btnfw.pack(pady=20)
    btnmon = Button(main_window, text="AIDE", font=('arial', 9, 'bold'), bg="light grey", fg="black", width="30",
                    height="2", command=lambda: monitor_window())
    btnmon.pack(pady=20)

    main_window.mainloop()


def process_window():
    global process_window
    process_window = Toplevel(main_window)
    process_window.title("Process Configuration")
    process_window.geometry("800x600")
    process_window.configure(bg="dark grey")
    label1 = Label(process_window, text="What Would you like to configure?", font=('arial', 12, 'bold'), bg="white",
                   fg="black")
    label1.pack(fill=X, pady=20)



def firewall_window():
    firewall_window = Toplevel(main_window)
    firewall_window.title("Firewall Configuration")
    firewall_window.geometry("1000x600")
    firewall_window.configure(bg="dark grey")
    label1 = Label(firewall_window, text="What Would you like to configure?", font=('arial', 12, 'bold'), bg="white",
                   fg="black")
    label1.pack(fill=X, pady=20)

    updatebtn = Button(firewall_window, text="View Current Iptables layout", font=('arial', 9, 'bold'), bg="light grey",
                       fg="black", width="30",
                       height="2", command=lambda: updateipt())
    updatebtn.pack(pady=20)


def monitor_window():
    monitor_window = Toplevel(main_window)
    monitor_window.title("AIDE Configuration")
    monitor_window.geometry("800x600")
    monitor_window.configure(bg="dark grey")
    label1 = Label(monitor_window, text="What Would you like to configure?", font=('arial', 12, 'bold'), bg="white",
                   fg="black")
    label1.pack(fill=X, pady=20)


def updateipt():
    text = Text(firewall_window, height=150, width=124)
    text.pack()
    with Popen(["iptables", '-L', '-v', '-n'], stdout=PIPE, stderr=PIPE) as p:
        for line in p.stdout:
            text.insert(END, line,)




main_window()

在代码中,函数名是,同时也是窗口名(顶级)。请更改所有函数名。另外,在
updatept
函数中,您提供了一个函数来放置文本小部件,以便它抛出错误

import os
import subprocess as sub
from tkinter import *
from subprocess import Popen, PIPE


def main_windows():
    global main_window
    main_window = Tk()
    main_window.title("Firewall test")
    main_window.geometry("1000x700")
    main_window.configure(bg="dark grey")
    label1 = Label(main_window, text="What Would you like to configure?", font=('arial', 12, 'bold'), bg="white",
                   fg="black")
    label1.pack(fill=X, pady=20)

    btnproc = Button(main_window, text="Process configuration", font=('arial', 9, 'bold'), bg="light grey", fg="black",
                     width="30", height="2", command=lambda: process_windows())
    btnproc.pack(pady=20)
    btnfw = Button(main_window, text="Firewall", font=('arial', 9, 'bold'), bg="light grey", fg="black", width="30",
                   height="2", command=lambda: firewall_windows())
    btnfw.pack(pady=20)
    btnmon = Button(main_window, text="AIDE", font=('arial', 9, 'bold'), bg="light grey", fg="black", width="30",
                    height="2", command=lambda: monitor_windows())
    btnmon.pack(pady=20)

    main_window.mainloop()


def process_windows():
    global process_window
    process_window = Toplevel()
    process_window.title("Process Configuration")
    process_window.geometry("800x600")
    process_window.configure(bg="dark grey")
    label1 = Label(process_window, text="What Would you like to configure?", font=('arial', 12, 'bold'), bg="white",
                   fg="black")
    label1.pack(fill=X, pady=20)



def firewall_windows():
    firewall_window = Toplevel()
    firewall_window.title("Firewall Configuration")
    firewall_window.geometry("1000x600")
    firewall_window.configure(bg="dark grey")
    label1 = Label(firewall_window, text="What Would you like to configure?", font=('arial', 12, 'bold'), bg="white",
                   fg="black")
    label1.pack(fill=X, pady=20)

    updatebtn = Button(firewall_window, text="View Current Iptables layout", font=('arial', 9, 'bold'), bg="light grey",
                       fg="black", width="30",
                       height="2", command=lambda: updateipt())
    updatebtn.pack(pady=20)


def monitor_windows():
    monitor_window = Toplevel(main_window)
    monitor_window.title("AIDE Configuration")
    monitor_window.geometry("800x600")
    monitor_window.configure(bg="dark grey")
    label1 = Label(monitor_window, text="What Would you like to configure?", font=('arial', 12, 'bold'), bg="white",
                   fg="black")
    label1.pack(fill=X, pady=20)


def updateipt():
    text = Text(main_window, height=150, width=124)
    text.pack()
    with Popen(["iptables", '-L', '-v', '-n'], stdout=PIPE, stderr=PIPE) as p:
        for line in p.stdout:
            text.insert(END, line)

    



main_windows()

为什么要将函数名作为
Text()
的父函数传递到
updatept()
中?