【问题标题】:How to update client labels from a server in python?如何从 python 中的服务器更新客户端标签?
【发布时间】:2019-08-31 18:02:41
【问题描述】:

我有一个客户端每 10 秒刷新一次服务器。当客户端与服务器签入时。我希望服务器将标签文本值发送回客户端以应用于客户端标签。

客户端发送一个序列号,一旦服务器验证它。服务器向客户端发回一条消息。我有一个 itemPrice 用作在连接中断之前在同一操作中发送的标签文本。我不知道如何在客户端检索 itemPrice。有没有办法标记要用于客户端变量的值?

服务器将保存客户端标签的文本。如何将此文本发送为客户端上的变量?

客户端.py

from tkinter import *
import socket


root = Tk()
# Variables
itemPrice1 = ''
itemPrice2 = ''


# SERVER CONNECTION
serialNumber = '001'

# Define Refresh Time
def Refresh():
    root.after(10000, Refresh)  # every 10 seconds...
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('localhost', 10000)
    sock.connect(server_address)
    sock.send(serialNumber.encode('UTF-8'))
    amount_received = 0
    data = sock.recv(16)
    amount_received += len(data)
    print('received "%s"' % data)

# Layout
root.config(background='black')
item1 = Label(root, text=itemPrice1)
item1.grid(row=0, column=0)
item1.config(background='grey', width=10)
closeButton = Button(root, text='Close', command=root.destroy)
closeButton.grid(row=1, column=0)


Refresh()
root.mainloop()

服务器.py

import socket
import data
import price

# VARIABLES
itemPrice1 = price.itemPrice1

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the port
server_address = ('localhost', 10000)
print('starting up on %s port %s' % server_address)
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)

while True:
    # Wait for a connection
    print('waiting for a connection')
    connection, client_address = sock.accept()
    try:
        print('connection from', client_address)

        # Receive the data in small chunks and retransmit it
        while True:
            dataRec = connection.recv(16)
            print(client_address, dataRec)
            if dataRec == data.serialNumber:
                print('sending data back to the client')
                connection.send('Serial Valid'.encode('UTF-8'))
                connection.send(itemPrice1)

                break
            else:
                connection.send('Key Not Valid'.encode('UTF-8'))
                break

    finally:
        # Clean up the connection
        connection.close()

价格.py

itemPrice1 = b'0.01'

数据.py

serialNumber = b'001'

【问题讨论】:

  • Edit您的问题并解释“有没有办法标记要用于客户端变量的值?”* 更详细.请举例说明您希望收到什么?
  • 了解module-json
  • 服务器能否向客户端发送一个 json 文件?还是需要另一个应用程序才能做到这一点?
  • "server be able to send a json": json 模块是标准的python,不需要其他模块/应用程序,所以是的

标签: python python-3.x tkinter client-server auto-update


【解决方案1】:

对于服务器。我删除了变量并直接从 price.py 转发价格

            if dataRec == data.serialNumber:
                print('sending data back to the client')
                connection.send('Serial Valid'.encode('UTF-8'))
                connection.send(price.itemPrice1.encode('UTF-8'))  # SENDING PRICE TO CLIENT FOR LABEL
                connection.send(price.itemPrice2.encode('UTF-8'))
                break

为客户。我将变量直接放在定义中。

def Refresh():
    root.after(10000, Refresh)  # every 10 seconds...
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_address = ('localhost', 10000)
    sock.connect(server_address)
    sock.send(serialNumber.encode('UTF-8'))
    amount_received = 0
    data = sock.recv(12)
    amount_received += len(data)
    print('received "%s"' % data)
    itemPrice1 = sock.recv(4)
    print('price', itemPrice1)
    itemPrice1Label.config(text=itemPrice1)
    itemPrice2 = sock.recv(4)
    print('price', itemPrice2)
    itemPrice2Label.config(text=itemPrice2)

这样做我必须定义 sock.recv() 数量,这样字符数就不会相互重叠。例如。

item price 1 = 1.05
item price 2 = 0.10

itemPrice1 = sock.recv(7)
would print 1.050.1
vs
itemPrice1 = sock.recv(4)
would print 1.05

【讨论】:

    猜你喜欢
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    • 2012-01-17
    • 2014-09-11
    相关资源
    最近更新 更多