刚开始学习Socket和Tkinter时,我想要一种更清洁,更好的方法来实现这一点。我想要获得的最重要的东西是自动调整标签大小,使其高度等于计数器的高度
from tkinter import *
import socket
#Initialize Tkinter
win = Tk()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Taking socket
information
server = "pythonprogramming.net"
port = 80
server_ip = socket.gethostbyname(server)
frame_server_inventory = Frame(win, relief="raised", borderwidth=3)
label_tell_ip = Label(frame_server_inventory, text="IP: " , borderwidth=3 ,
width=6, relief="raised",anchor="center")
label_tell_ip.grid(row=0 , column = 0) #Ip indicator
label_tell_header = Label(frame_server_inventory,
text="Header",borderwidth=3,height=24,width=6, relief="raised")
label_tell_header.grid(row=18 , column=0)
frame_server_inventory.grid()
get_request = "GET / HTTP/1.1\nHost:"+server+"\n\n"
sock.connect((server,port))
sock.send(get_request.encode())
result = sock.recv(2048)
frame_server_item= Frame(win,relief="raised",borderwidth=3)
frame_server_item.grid(row=0 , column=1)
label_ip = Label(frame_server_item, text=server_ip,borderwidth=3, width=70 ,
relief="raised")
label_ip.grid(row=0) # Displays Server IP
label_header = Label(frame_server_item, text= result,
borderwidth=3,height=24,width=70 , relief="raised")
label_header.grid(row=1)
#Keeps The loop running
win.mainloop()