简体   繁体   English

我无法将GUI插入到我的python程序中

[英]I am having trouble incorparating a GUI into my python program

I'm sure it's a simple fix but I'm just spacing out here on my basics. 我确定这是一个简单的修复,但我只是在我的基础上分开。 I need to incorporate a Gui that simply pops up and states that a connection has been made between the client and the server. 我需要合并一个简单弹出的Gui,并声明客户端和服务器之间已建立连接。

I can get the GUI to pop up when it is on top of my code with all of my variables but It won't run underneath my code which is where the connection that I need it to display is defined. 我可以使用我的所有变量在我的代码之上弹出GUI但它不会在我的代码下面运行,这是我需要它显示的连接的定义。

# it will run but (address) is not defined yet
import socket
from tkinter import *

root = Tk()
theLabel = Label(root,text="Connection from {address} has been established.")
theLabel.pack()
root.mainloop()

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostname(), 1234))
s.listen(5)

while True:
  clientsocket, address = s.accept()
  print(f"Connection from {address} has been established.")
  clientsocket.send(bytes("HELL YEAH FAM!!! WE DID IT!!","utf-8"))
  clientsocket.close()

it has no error message, it just won't run the GUI. 它没有错误消息,它只是不会运行GUI。

You have to configure everything, then you can call a function for a connection and then at the end call root.mainloop(). 您必须配置所有内容,然后您可以为连接调用函数,然后在最后调用root.mainloop()。 Here are some of the work you need to make: 以下是您需要完成的一些工作:

from socket import AF_INET, SOCK_STREAM, socket, gethostname
from tkinter import *
from tkinter import ttk

IP = gethostname() # or "127.0.0.1"
PORT = 1337

root = Tk()
root.title("")
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

for child in mainframe.winfo_children(): 
child.grid_configure(padx=5, pady=5)

root.bind('<Return>', connectionFunc)

def connectionFunc(*args):
    # this way you dont have to close the socket.
    with socket(AF_INET, SOCK_STREAM) as s:
        s.listen()
        s.bind((IP, PORT))
        conn, addr = s.accept()
        with conn:
            print(f"connection from: {addr}")
            while True:
                data = conn.recv(1024)
                if not data:
                    break
                conn.sendall(data)

root.mainloop()

You should use thread for waiting connection: 你应该使用线程来等待连接:

import socket
import threading
from tkinter import *

def wait_connection():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((socket.gethostname(), 1234))
    s.listen(5)

    while True:
        clientsocket, address = s.accept()
        msg.set(f"Connection from {address} has been established.")
        clientsocket.send(bytes("HELL YEAH FAM!!! WE DID IT!!","utf-8"))
        clientsocket.close()

root = Tk()
msg = StringVar(value='Waiting for connection ...')
theLabel = Label(root,textvariable=msg)
theLabel.pack()

# start a thread for waiting client connection
threading.Thread(target=wait_connection, daemon=True).start()

root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM