2017-12-04 4 views
0

クライアントプログラムがサーバーから受信したメッセージに対応する名前のイメージを表示しようとしています。だから、イメージのパスに変数を追加する

クライアントはb'red」はパス上に画像を表示すべき受信した場合:ここでは 『/home/pi/Desktop/gifs/red.gif』

は、クライアントプログラムのコードです:私は、メッセージ内の情報を受け取り、私のイメージへのパスで変数を作るにはどうすればよい

import socket 
import tkinter as tk 
HOST = 'localhost' # The remote host 
PORT = 50007    # The same port as used by the server 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.connect((HOST, PORT)) 

message = s.recv(1024) 
print(message) # Message is a bytes-like-object e.g. b'red' 

root = tk.Tk() 
image1 = tk.PhotoImage(file="/home/pi/Desktop/gifs/x.gif") 
#x should change accordingly to the received message 
Thelabel = tk.Label(image=image1) 
Thelabel.pack() 
root.update() 

?だから私はバイトのようなオブジェクトを文字列にデコードする必要があると思いますか?

+2

[バイト文字列をデコードしてフォーマットするのはなぜですか](https://ideone.com/wkC6lQ) – CommonSense

答えて

0

@CommonSenseの質問に基づいています。

byte_string = s.recv(1024) 
    path = '/home/pi/Desktop/gifs/%s.gif' % byte_string.decode() 

    root = tk.Tk() 
    image1 = tk.PhotoImage(file=path) 
    Thelabel = tk.Label(image=image1) 
    Thelabel.pack() 
    root.update() 
関連する問題