2016-04-23 36 views
0

如果有人真的讀到這個謝謝! 無論如何,我每次運行代碼時都會遇到一個'AttributeError',我找不到問題所在。我使用Socket,tKinter,os和multiprocessing。這裏是我的代碼(我知道它現在最pythony的事情在世界上,但嘿,我剛剛與插座扮演):Socket,AttributeError:'str'對象沒有屬性'send'

#---Import statments---# 
import socket, os, multiprocessing 
import tkinter as tk 

#---global variables---# 
setup = '' 
cleintsocket = '' 

#---Defs---# 
def setup(): 
    global host, port, user 
    host = setup_host_box.get() 
    port = setup_port_box.get() 
    user = setup_user_box.get() 

def connect(self, hostname, connectingport): 
    self.connect((hostname, int(connectingport))) 
    print('connected') 
    multiprocessing.Process(target = resv()).start() 

def create_sock(nhost, nport): 
    global cleintsocket 
    cleintsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    connect(cleintsocket, nhost, nport) 

def send(username, cleintsock): 
    '''to send a message''' 
    usrmsg = (username + ' - ' + chat_msg_box.get()).encode() 
    cleintsock.send(usrmsg) 

def resv(sock): 
    '''resive subscript, run through mutiprosses module''' 
    while True: 
     rmsg = sock.recv(1024).decode() 
     chat_msg_display_text.insert('end.0.', rmsg) 

def chat(): 
    '''loads chat page''' 
    setup_host_text.pack_forget() 
    setup_host_box.pack_forget() 
    setup_port_text.pack_forget() 
    setup_port_box.pack_forget() 
    setup_user_text.pack_forget() 
    setup_user_box.pack_forget() 
    setup_confirm_button.pack_forget() 
    chat_msg_desplay_text.pack() 
    chat_msg_box.pack() 
    chat_msg_send_button.pack() 

def start(): 
    '''starts the setup page''' 
    setup_host_text.pack() 
    setup_host_box.pack() 
    setup_port_text.pack() 
    setup_port_box.pack() 
    setup_user_text.pack() 
    setup_user_box.pack() 
    setup_confirm_button.pack() 

#---TK Setup---# 
#--window setup--# 
window = tk.Tk() 
window.title('Chat') 
window.geometry('600x600') 
window.configure(background='#ffffff') 
#--connection setup page--# 
setup_host_text = tk.Label(window, text = 'Host') 
setup_host_box = tk.Entry(window, bg = '#ffffff') 
setup_port_text = tk.Label(window, text = 'Port') 
setup_port_box = tk.Entry(window, bg = '#ffffff') 
setup_user_text = tk.Label(window, text = 'Username') 
setup_user_box = tk.Entry(window, bg = '#ffffff') 
setup_confirm_button = tk.Button(window,text = 'Connect', command = setup()) 
#--chat page--# 
chat_msg_box = tk.Entry(window, bg='#ffffff') 
chat_msg_send_button = tk.Button(window, text = 'send', command = send(user, cleintsocket)) 
chat_msg_display_text = tk.Text(window, width=600, height=500, wrap = 'word') 
#--------------# 

start() 

蟒蛇控制檯是說這裏存在chat_msg_send_button = tk.Button(window, text = 'send', command = send(user, cleintsocket))然而,能產生AttributeError: 'str' object has no attribute 'send'錯誤的錯誤我看不到任何問題。

請幫忙。

再次感謝!

編輯:這裏的錯誤(現在還沒有必要的,但這個是原則)

Traceback (most recent call last): 
    File ".../tkcleint.py", line 76, in <module> 
    chat_msg_send_button = tk.Button(window, text = 'send', command = send(user, cleintsocket)) 
    File ".../tkcleint.py", line 29, in send 
    cleintsock.send(usrmsg) 
AttributeError: 'str' object has no attribute 'send' 
+0

首先你需要在提高'AttributeError'的行中使用'lambda'函數,像這樣:'chat_msg_send_button = tk.Button(window,text ='send',command = lambda:send(user,cleintsocket ))'。如果解決了您的問題,請告訴我,我會將其作爲答案發布。 –

回答

0

第一關(如@ R.Murry指出)您呼叫的功能立即並通過他們的迴歸值作爲命令,在這種情況下None,所以我想通過固定啓動起來:

setup_confirm_button = tk.Button(window,text = 'Connect', command = setup) #don't call setup here 
... 
def send_button_callback(): 
    send(user, cleintsocket) 
chat_msg_send_button = tk.Button(window, text = 'send', command = send_button_callback) 

旁邊,它包括整個錯誤是非常重要的消息在你的問題,因爲這是不運行到的問題是:你傳遞變量cleintsocketsend,並嘗試使用一個插座的.send方法

Traceback (most recent call last): 
    File ".../test.py", line 76, in <module> 
    chat_msg_send_button = tk.Button(window, text = 'send', command = send(user, cleintsocket)) 
    File ".../test.py", line 29, in send 
    cleintsock.send(usrmsg) 
AttributeError: 'str' object has no attribute 'send' 

,但是它被初始化爲空字符串:

cleintsocket = '' 

因此,如果調用send之前更改爲socket你會得到錯誤,只需檢查它是否已經被初始化尚未:

def send(username, cleintsock): 
    '''to send a message''' 
    if cleintsock: #not an empty string 
     usrmsg = (username + ' - ' + chat_msg_box.get()).encode() 
     cleintsock.send(usrmsg) 
    #else:window.bell() #play a error beep 
+0

@Taghg McDonald-Jensen感謝您的幫助!現在它工作正常。 –

+0

那麼這工作正常 –

+0

感謝@R。 Murray也有幫助 –