我正试图用蟒蛇在gui上建立一个随机数游戏

时间:2015-03-23 21:08:07

标签: python tkinter

我已经开始为我的随机数游戏构建一个客户端服务器gui,我有代码让它猜测数字,但我不能得到它我的名字输入的值和猜测等 这是客户端

from tkinter import *
from socket import *
from sys import argv

#create a socket
client_socket = socket(AF_INET, SOCK_STREAM)


def show_entry_fields(): #when this called it will show the name and what is in the label called name
text.insert(INSERT,"Name: %s \n" % (e1.get()) )
def show_entry_fields2():#when this called it will show the name and what is in the label called guess
text.insert(INSERT,"Guess: %s  \n" % (e2.get()) )
def show_entry_fields3():
#when this called it will show the name and what is in the label called  continue
text.insert(INSERT,"Continue: %s " % (e3.get()) )
#print("First Name: %s\nLast Name: %s" % (e1.get(), e2.get()))




# argument to store the port number
port = int(argv[1])

finished = False
while not finished:


 #connect to the server at the specified address
    client_socket.connect(('localhost', port))

    #connected ok
    finished = True

top = Tk()


text = Text(top)
text.insert(INSERT, "What is your name?\n")
#text.insert(END, "Bye Bye.....")
text.pack()

master = Tk()
Label(master, text=" Name").grid(row=0)
# puts the label with name on the grid at the spot it is allocated
Label(master, text=" Guess").grid(row=1)
# puts the label with guess on the grid at the spot it is allocated
Label(master, text=" Continue").grid(row=2)
# puts the label with continue on the grid at the spot it is allocated

e1 = Entry(master)
e2 = Entry(master)
e3 = Entry(master)

e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=2, column=1)

Button(master, text='Quit', command = master.quit).grid(row=3, column=0, sticky=W, pady=4)
Button(master, text='Name', command = show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)
Button(master, text='Guess', command = show_entry_fields2).grid(row=3, column=2, sticky=W, pady=4)
Button(master, text='Continue', command = show_entry_fields3).grid(row=3, column=3, sticky=W, pady=4)


#send some text
connected = True
while connected:
    message = Label
    data = message.encode()
    client_socket.send(data)

    mainloop() 

下一部分是服务器

#echo server
import random
from socket import *
from sys import argv


#create a socket
server_socket = socket(AF_INET, SOCK_STREAM)

#allows address to be reused
server_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)

# checks the number of command line arguments
print ('Number of arguments ', len(argv))


#remember the port number
port = int(input('Enter port number: '))


server_socket.bind(('localhost', port))


#listen for connections
server_socket.listen(1)

#accept a connection from the client
connection, client_address = server_socket.accept()
print('Accepted a connection from: ', client_address)

finished = False
while not finished:

    def game():
        yes = ['y', 'Y', 'Yes', 'yes']
        number = random.randint(1, 15)
        numAttempts = 0

    #receive a message from the client
    data_in = connection.recv(1024)
    message = data_in.decode()
    if (message == 'quit'):
        finished = True
    else:
        # send back the response.
        data = message.encode()
        connection.send(data)

当我运行代码时出现错误 Traceback(最近一次调用最后一次):   文件“gui2.py”,第64行,in     data = message.encode() AttributeError:类型对象'Label'没有属性'encode' 谁会知道如何获取代码来编码名称的值并将其传输到服务器?

1 个答案:

答案 0 :(得分:0)

在客户端代码中,您正在编码Label对象,该对象没有此方法。

while connected:
    message = Label
    data = message.encode()

您不想对返回的值进行编码(e1?)