我一直在研究一个在2个树莓派之间运行的项目。我发现当我用套接字代码运行Tkinter代码时,它将首先运行套接字代码。这是一个问题,因为我希望屏幕显示它正在尝试连接到客户端,但是菜单和Tkinter代码仅在建立连接后才会显示。请有人帮忙,这是一些代码: 您可以看到由哈希轮廓45、46、47构成的GUI IVE是麻烦制造者。
from tkinter import *
import socket as connect
import sys
host = ''
port = 5490
wn = Tk()
wn.geometry('800x450+300+100')
wn.title('Connect 4 Server')
BordF = Frame(wn)
BordF.pack(side=TOP, fill=BOTH)
font = 'arial 200'
font2 = 'arial 15'
LeftBorderFrame = Frame(BordF, bg='#93D5FF')
LeftBorderFrame.pack(side=LEFT, fill = Y)
LeftBorderSpacer1 = Label(LeftBorderFrame, text=' ', bg='#93D5FF', font=font)
LeftBorderSpacer1.pack(side=LEFT)
RightBorderFrame = Frame(BordF, bg='#93D5FF')
RightBorderFrame.pack(side=RIGHT, fill = Y)
RightBorderSpacer1 = Label(RightBorderFrame, text=' ', bg='#93D5FF', font=font)
RightBorderSpacer1.pack(side=RIGHT)
TopBorderFrame = Frame(BordF, bg='#93D5FF')
TopBorderFrame.pack(side=TOP, fill = X)
TopBorderSpacer1 = Label(TopBorderFrame, text='', bg='#93D5FF', font=font2)
TopBorderSpacer1.pack(side=TOP)
BottomBorderFrame = Frame(BordF, bg='#93D5FF')
BottomBorderFrame.pack(side=BOTTOM, fill = X)
BottomBorderSpacer1 = Label(BottomBorderFrame, text='', bg='#93D5FF', font=font2)
BottomBorderSpacer1.pack(side=BOTTOM)
SwitchF = Frame(BordF)
SwitchF.pack(side=TOP)
ConnGF = Frame(SwitchF)
ConnGF.pack(side=TOP)
InfoL1 = Label(ConnGF, text='Waiting for Connection \n \n ' + connect.gethostbyname(connect.gethostname()), font='arial 20')
InfoL1.pack(side=TOP)
s = connect.socket(connect.AF_INET, connect.SOCK_STREAM)
s.bind((host, port))
#
s.listen(1)
conn, adrr = s.accept()
print('Connected to: ' + adrr[0] + ': ' + adrr[1])
#
wn.mainloop()
答案 0 :(得分:1)
问题是mainloop()
显示GUI的代码行在连接代码之后。另外,mainloop()
是一个无休止的循环,因此在GUI销毁之前,(套接字连接)之后编写的任何代码都不会发生。
解决方案::您可以使用tk.after()
延迟该方法。这将在执行给定方法之前等待给定的毫秒数:
from tkinter import *
import socket as connect
import sys
host = ''
port = 5490
def connect():
s = connect.socket(connect.AF_INET, connect.SOCK_STREAM)
s.bind((host, port))
#
s.listen(1)
conn, adrr = s.accept()
print('Connected to: ' + adrr[0] + ': ' + adrr[1])
#
wn = Tk()
wn.geometry('800x450+300+100')
wn.title('Connect 4 Server')
BordF = Frame(wn)
BordF.pack(side=TOP, fill=BOTH)
font = 'arial 200'
font2 = 'arial 15'
LeftBorderFrame = Frame(BordF, bg='#93D5FF')
LeftBorderFrame.pack(side=LEFT, fill = Y)
LeftBorderSpacer1 = Label(LeftBorderFrame, text=' ', bg='#93D5FF', font=font)
LeftBorderSpacer1.pack(side=LEFT)
RightBorderFrame = Frame(BordF, bg='#93D5FF')
RightBorderFrame.pack(side=RIGHT, fill = Y)
RightBorderSpacer1 = Label(RightBorderFrame, text=' ', bg='#93D5FF', font=font)
RightBorderSpacer1.pack(side=RIGHT)
TopBorderFrame = Frame(BordF, bg='#93D5FF')
TopBorderFrame.pack(side=TOP, fill = X)
TopBorderSpacer1 = Label(TopBorderFrame, text='', bg='#93D5FF', font=font2)
TopBorderSpacer1.pack(side=TOP)
BottomBorderFrame = Frame(BordF, bg='#93D5FF')
BottomBorderFrame.pack(side=BOTTOM, fill = X)
BottomBorderSpacer1 = Label(BottomBorderFrame, text='', bg='#93D5FF', font=font2)
BottomBorderSpacer1.pack(side=BOTTOM)
SwitchF = Frame(BordF)
SwitchF.pack(side=TOP)
ConnGF = Frame(SwitchF)
ConnGF.pack(side=TOP)
InfoL1 = Label(ConnGF, text='Waiting for Connection \n \n ' + connect.gethostbyname(connect.gethostname()), font='arial 20')
InfoL1.pack(side=TOP)
#wait two seconds before connecting
wn.after(2000, connect)
wn.mainloop()
``