I am running Python 3.2.2 and writing code for socket testing. For testing convenience, I use Tkinter to add a GUI. I have yet to figure out how to update the information in the grid used. I want to update "host2" and "port2" in the functions "change1" and "change3" in the following code:
import socket from tkinter import * import tkinter.simpledialog root = Tk() root.title("Server") root.iconbitmap("etc.ico") root.geometry("350x100+200+200") frame = Frame(root) host1 = Label(frame,text="Host: ").grid(row=0,column=0) port1 = Label(frame,text="Port: ").grid(row=1,column=0) HOST = 'localhost' PORT = 11111 STATUS = 'EMPTY' host2 = Label(frame,text=HOST,width=10).grid(row=0,column=1) port2 = Label(frame,text=PORT,width=10).grid(row=1,column=1) status1 = Label(root,text=STATUS) status1.pack(side=RIGHT,padx=2,pady=2) def change1(): global HOST HOST= tkinter.simpledialog.askstring(title="Host",prompt="Enter the IP of the Host.") host2.grid_forget() def change3(): global PORT PORT= tkinter.simpledialog.askinteger(title="Port",prompt="Enter the Port of the IP.") port2.grid_forget() def go1(): global HOST global PORT home = socket.socket(socket.AF_INET, socket.SOCK_STREAM) home.bind((HOST, PORT)) home.listen(1) conn, addr = home.accept() print (addr) while 1: data = conn.recv(1024) if not data: break global STATUS STATUS = data.decode('UTF-8') conn.send(bytes('Received "Hello World"','UTF-8')) conn.close() global status1 status1.pack_forget() status1.pack(side=RIGHT,padx=2,pady=2) change = Button(frame, text="Change Host", width=10,command=change1).grid(row=0,column=2) change2 = Button(frame, text="Change Port", width=10,command=change3).grid(row=1,column=2) go = Button(frame, text="GO!",command=go1,width =10).grid(row=2,column=2) frame.pack(side=LEFT) mainloop()
Any help on this would be greatly appreciated! Thanks!
source share