0

I created a Top level widget with a text widget and a button, How do I insert text into the Text widget with a click of a button.

My Code:

def Networking():
    top = Toplevel()
    top.title("Networking")
    top.geometry("800x800")

    style = ttk.Style(root)
    style.configure('l.TFrame', background="gray94")

    command_frame = ttk.Frame(top, style="l.TFrame")
    command_frame.grid()
    command_frame.place(x=30, y=269)
    
    network_lbl = Image.open(rf'{current_dir}/img/network.png')
    resize_image = network_lbl.resize((200, 200))
    networklogo = ImageTk.PhotoImage(resize_image)
    network_logo = ttk.Label(top, image=networklogo, style="l.TLabel")
    network_logo.image = networklogo
    network_logo.grid(column=0, row=0, pady=5)
    network_logo.place(x=89, y=40)

    def netstat_command():
        netstat_an = commands.netstat.Netstat_an()
        return network_text.insert("end", netstat_an)

    network_text = scrolledtext.ScrolledText(top, width=69, height=20)
    network_text.config(state="disabled", bg="black", fg="white")
    network_text.place(x=30, y=300)

    netstat_btn = ttk.Button(command_frame, text="netstat -an", command=lambda: netstat_command)
    netstat_btn.grid(column=1, row=0)

    ipconfig_btn = ttk.Button(command_frame, text="ipconfig", command="")
    ipconfig_btn.grid(column=0, row=0)


    top.mainloop()

I don't get any errors, but the network_text.insert() code isn't working, I'm probably utilizing the function and the lambda wrong. Any way of doing this?

Mike Cash
  • 317
  • 4
  • 13
  • 1
    You are not executing the function inside the lambda because of missing `()`. For your case, simply remove `lambda:`, i.e. `command=netstat_command`. – acw1668 Jan 31 '22 at 05:34
  • Thank you for replying, I also tried what you said and it's still not inserting anything into the Text widget. – Mike Cash Jan 31 '22 at 05:36

2 Answers2

1

So I found the solution to my problem, for some reason network_text.config(state="disabled") was disabling the widget before any text could be inserted, removing the disabled state part worked.

Mike Cash
  • 317
  • 4
  • 13
0

Try to print the netstat_an variable in the funcion netstat_command, if is print empty you need check your commands.netstat.Netstat_an(). And don't forget the brackets for the lambda at command=lambda: netstat_command()

def Networking():
    top = Toplevel()
    top.title("Networking")
    top.geometry("800x800")

    style = ttk.Style(root)
    style.configure('l.TFrame', background="gray94")

    command_frame = ttk.Frame(top, style="l.TFrame")
    command_frame.grid()
    command_frame.place(x=30, y=269)

    network_lbl = Image.open(rf'{current_dir}/img/network.png')
    resize_image = network_lbl.resize((200, 200))
    networklogo = ImageTk.PhotoImage(resize_image)
    network_logo = ttk.Label(top, image=networklogo, style="l.TLabel")
    network_logo.image = networklogo
    network_logo.grid(column=0, row=0, pady=5)
    network_logo.place(x=89, y=40)

    def netstat_command():
        netstat_an = commands.netstat.Netstat_an()
        print(netstat_an)
        return network_text.insert("end", netstat_an)

    network_text = scrolledtext.ScrolledText(top, width=69, height=20)
    network_text.config(state="disabled", bg="black", fg="white")
    network_text.place(x=30, y=300)

    netstat_btn = ttk.Button(command_frame, text="netstat -an", command=lambda: netstat_command)
    netstat_btn.grid(column=1, row=0)

    ipconfig_btn = ttk.Button(command_frame, text="ipconfig", command="")
    ipconfig_btn.grid(column=0, row=0)


    top.mainloop()
Aram SEMO
  • 146
  • 8