Why Can't change a GUI text while using telnetlib In Python

239 views Asked by At

I'm trying to make my first program in Python. It's a program for remote shutdown using telnet. The code works ok when output is set to console, but I'm trying to make a little interface using tkinter. A button and a scroll text box.

I can't do a T.insert from inside the shutdownAll() function. The flow passes over it and continues with tn = telnetlib.Telnet(host,port,timeout).

But If I remove the tn = telnetlib.Telnet(host,port,timeout), and all the telnet related stuff it works ok.

What i'm missing? Thanks for your patience.

from tkinter import *
import csv, telnetlib, socket, sys

def shutdownAll():
    for index,row in enumerate(addressList):
        T.insert(END, "This doesn't work")
        print ('Trying to establish connection with: ' + row[4])
        print ('Host: ' + row[0] + ', Username: ' + row[1] + ', Password: ' + row[2] + ', OS: ' + row[3])
        host = row[0]
        username = row[1]
        password = row[2]
        os = row[3]
        computerName = row[4]

        try :
            tn = telnetlib.Telnet(host,port,timeout)
            tn.read_until(login_prompt, timeout)
            tn.write(username.encode('ascii') + b"\n")

            if password :
                tn.read_until(password_prompt,timeout)
                tn.write(password.encode('ascii') + b"\n")

            print ('Sending shutdown command to: ' + computerName)
            if os == "linux" :
                tn.write("sudo poweroff\n")
            elif os == "windows" :
                tn.write("shutdown /s\n")
            tn.close()

        except socket.timeout :
            print("ERROR! Can't connect")
            T.insert(END, "Inside the shutdown")


#VARIABLES
tn = None
host = "192.192.1.39"
port = 23
timeout = 10
username = "pi"
password = "raspberry"
os = "linux"
computerName = "Test"
login_prompt = b"login: "
password_prompt = b"Password: "
addressList = csv.reader(open('computers.csv', 'r'))


#GUI WINDOW
window = Tk()
window.title("Remote Shutdown utility")
window.geometry("500x500")
btn = Button(window, text="Shutdown all computers now!", command=shutdownAll)
btn.pack()

S = Scrollbar(window)
T = Text(window, height=4, width=50)
S.pack(side=RIGHT, fill=Y)
T.pack(side=LEFT, fill=Y)
S.config(command=T.yview)
T.config(yscrollcommand=S.set)
T.insert(END, "This a test")

window.mainloop()
0

There are 0 answers