【问题标题】:tkinter text widget not able to appear on desired windowtkinter 文本小部件无法出现在所需的窗口上
【发布时间】:2021-08-09 09:27:35
【问题描述】:

所以我编写了一个简单的 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,)

text = Text(main_window, height=150, width=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()

【问题讨论】:

  • 为什么将函数名作为Text()的父级传递给updateipt()

标签: python linux tkinter


【解决方案1】:

在您的代码中,您的函数名称是也是您的窗口名称(顶层)。请更改所有函数名称。此外,在您的 updateipt 函数中,您提供了一个放置 Text 小部件的函数,因此它会引发错误。

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多