我一直试图通过tkinter gui连接到telnet。没有tkinter gui,我的代码没有错误,但是它无法通过tkinter gui上的按钮连接到telnet。 tkinter gui我缺少什么?
直接致电
import os, time, re, telnetlib
from tkinter import *
def Load():
# Telnet
host = 'localhost'
port = 14440
path = 'C:/IPG/hil/win32-7.0/bin'
# Change path and open a software
os.chdir(path)
opencm = 'CM.exe -cmdport ' + str(port)
os.system(opencm)
# Connect to telnet
tn = telnetlib.Telnet(host, port)
tn.open(host, port)
#
cmd ='LoadTestRun "Examples/VehicleDynamics/Rollover/FishHook"'
tn.write((cmd + '\n').encode('ascii'))
if __name__ == '__main__':
# Call 'Load' directly
Load()
通过GUI上的按钮致电
import os, time, re, tenetlib
from tkinter import *
def Load():
# Telnet
host = 'localhost'
port = 14440
path = 'C:/IPG/hil/win32-7.0/bin'
# Change path and open a software
os.chdir(path)
opencm = 'CM.exe -cmdport ' + str(port)
os.system(opencm)
# Connect to telnet
tn = telnetlib.Telnet(host, port)
tn.open(host, port)
#
cmd ='LoadTestRun "Examples/VehicleDynamics/Rollover/FishHook"'
tn.write((cmd + '\n').encode('ascii'))
if __name__ == '__main__':
# Call 'Load' via button on gui
root = Tk()
root.geometry('100x100')
root.title('Test')
# Button
testrun_button = Button(root, text = 'Load', command = Load)
testrun_button.place(x = 20, y = 50)
root.mainloop()
执行代码2后,出现错误消息“ ConnectionRefusedError:[WinError 10061”], 第14行,在Load tn = telnetlib.Telnet(host,port)