我想爲我的抽搐聊天機器人做一個測試Tkinter按鈕。單擊此按鈕時會執行的操作是向其所連接的通道發送一條簡單消息。然而,當我試圖弄清楚如何做到這一點時,我遇到了一些問題。我已經創建了基本的按鈕,但點擊它,當我得到這個控制檯錯誤:Python Tkinter按鈕問題(抽搐機器人)
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
File "C:\Users\Cosmicluck\Desktop\bot.py", line 122, in commandTest
send_message(CHAN, "Click!")
File "C:\Users\Cosmicluck\Desktop\bot.py", line 30, in send_message
con.send(bytes('PRIVMSG %s :%s\r\n' % (chan, msg), 'UTF-8'))
NameError: name 'con' is not defined
現在,它說「騙子」沒有定義,但我並不需要去定義它,因爲這些命令我已成立工作得很好。這裏是NameError的引用代碼:
def send_pong(msg):
con.send(bytes('PONG %s\r\n' % msg, 'UTF-8'))
def send_message(chan, msg):
con.send(bytes('PRIVMSG %s :%s\r\n' % (chan, msg), 'UTF-8'))
def send_nick(nick):
con.send(bytes('NICK %s\r\n' % nick, 'UTF-8'))
def send_pass(password):
con.send(bytes('PASS %s\r\n' % password, 'UTF-8'))
def join_channel(chan):
con.send(bytes('JOIN %s\r\n' % chan, 'UTF-8'))
def part_channel(chan):
con.send(bytes('PART %s\r\n' % chan, 'UTF-8'))
最後,這裏是我的Tkinter按鈕代碼:
master = Tk()
def commandTest():
send_message(CHAN, "Click!")
b = Button(master, text="OK", command=commandTest)
b.pack()
mainloop()
我希望有人能解釋這可能是固定的,如果它甚至有可能去做。謝謝!
如果Python說它沒有被定義,它沒有被定義。請提供[MCVE](http://stackoverflow.com/help/mcve)。 – TigerhawkT3
「但是我不需要定義它,因爲我設置的命令工作得很好」。我不明白。你說你得到一個錯誤,所以它不能工作得很好。 – Kevin
代碼中的'con'在哪裏...有人看到它了? –