Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
862 views
in Technique[技术] by (71.8m points)

runtime error - Multithreading-Tkinter-Server_main thread is not in main loop

This is the Server-Script, which I need for my "database", to send files. But the Problem is not the socket-connection. You can find the whole ERROR under the Code.

import os,time,sys,random
from tkinter import *
from threading import Thread
from functools import partial
import tkinter,socket
#own modules
from daten import library
#

try:
    os.system("cls")
except Exception:
    os.system("clear")

def set_status_label(root):
    __status = tkinter.Label(root)
    __status.configure(text=library.home_status__text_bereit())
    __status.configure(bg=library.home_status_bg())
    __status.configure(fg=library.home_status_fg(),anchor=E,pady=2)
    __status.configure(font=library.home_status_font())
    __status.pack(side=BOTTOM,ipady=2,fill=X)
    return __status

def set_socket():
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) #TCP
    return s

def accept_connections(__status,Textfeld,root):
    Textfeld.configure(state='normal')
    text = library.home_textfeld_text_accept_conn()
    Textfeld.insert(INSERT,"
%s
"%(text))
    Textfeld.configure(state='disabled')
    while True:
        (client,addr) = db.accept()
        Textfeld.configure(state='normal')
        text = library.home_textfeld_text_connected()
        Textfeld.insert(INSERT,"%s %s
"%(text,str(addr)))
        Textfeld.configure(state='disabled')
        Thread(target=handle_client, args=(client,Textfeld,__status)).start()

def handle_client(client,Textfeld,__status):
    Textfeld.configure(state='normal')
    text = library.home_textfeld_text_accept_conn()
    Textfeld.insert(INSERT,"%s"%(text))
    Textfeld.configure(state='disabled')
    client.close()
    db.close()

def start(root,__status,__start_button,textfeld):
    global db
    db = set_socket()
    Textfeld.configure(state='normal')
    IP = library.socket_data_addr_IP()
    PORT = library.socket_data_addr_PORT()
    Textfeld.insert(INSERT,"Database-Address: %s:%s
"%(IP,PORT))
    Textfeld.insert(INSERT,"%s"%(library.home_textfeld_text_connect()))
    Textfeld.configure(state='disabled')
    try:
        db.bind(library.socket_data_addr())
        db.listen(library.socket_max_connections())
        Textfeld.configure(state='normal')
        Textfeld.insert(INSERT,"Abgeschlossen")
        Textfeld.configure(state='disabled')
    except Exception as e:
        Textfeld.configure(state='normal')
        Textfeld.insert(INSERT,"%s
"%(e))
        Textfeld.configure(state='disabled')

def __start_datenbank(root,__status,__start_button,textfeld):
    Textfeld = textfeld
    __start_button.destroy()
    __status.config(text=library.home_status__text_starten())
    global db
    db = start(root,__status,__start_button,textfeld)

def starte_knopf(root,__status,Textfeld):
    __start_button = tkinter.Button(root,text=library.home_start_button_text())
    __start_button.configure(font=library.home_start_button_font())
    start_command = partial(__start_datenbank,root,__status,__start_button,Textfeld)
    __start_button.configure(command=start_command)
    __start_button.pack(side=TOP,ipady=5)
    return __start_button

def configure_window(root):
    root.title(library.home_title())
    root.minsize(library.home_width(),library.home_height())
    root.maxsize(library.home_width(),library.home_height())
    root.configure(bg=library.home_bg())
    __status = set_status_label(root)
    #Textfeld
    Textfeld = tkinter.Text(root,width=library.home_textfeld_width())
    Textfeld.configure(bg=library.home_textfeld_bg())
    Textfeld.configure(fg=library.home_textfeld_fg())
    Textfeld.configure(font=library.home_textfeld_font())
    Textfeld.configure(relief='flat')
    Textfeld.pack(side=BOTTOM,ipady=5)
    Textfeld.configure(state='disabled')
    #
    __start_button = starte_knopf(root,__status,Textfeld)
    return (__status,__start_button,Textfeld)

root = tkinter.Tk()
(__status,__start_button,Textfeld) = configure_window(root)
#
ACCEPT_THREAD = Thread(target=accept_connections,args=(__status,Textfeld,root))
ACCEPT_THREAD.start()
ACCEPT_THREAD.join()
#
root.mainloop()

My ERROR:

RuntimeError: main thread is not in main loop

I searched the problem on the Internet and commented the Problem-Lines out but then I got this ERROR: NameError: name 'db' is not defined

question from:https://stackoverflow.com/questions/65647373/multithreading-tkinter-server-main-thread-is-not-in-main-loop

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should not call ACCEPT_THREAD.join() as it will block the application from executing root.mainloop() until the thread completed.

Also you should create ACCEPT_THREAD inside __start_datenbank() function instead of in the main block:

def __start_datenbank(root, __status, __start_button, textfeld):
    Textfeld = textfeld
    __start_button.destroy()
    __status.config(text=library.home_status__text_starten())
    start(root, __status, __start_button, textfeld)
    ACCEPT_THREAD = Thread(target=accept_connections, args=(__status,Textfeld,root))
    ACCEPT_THREAD.start()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...