Błąd interfejsu użytkownika twórcy serwera www Python

0

Pytanie

Mój program twórcą serwera www z interfejsem użytkownika i chcę uzyskać dane z Text klasa w pakiecie tkinter i z powrotem na serwer flask, ale program pokazuje podwójne okna, a serwer flask nic nie pokazuje.

try:
    import sys
    import platform
    from flask import Flask
    from typing import List
    from time import strftime
    from datetime import date
    from subprocess import run
    from webbrowser import open
    from consts import CONST_COLORS
    from builtins import (str , int ,Exception)
    from tkinter import font
    from tkinter.tix import Button
    from tkinter.ttk import (Label , Entry , Notebook , Frame , Combobox)
    from tkinter.__init__ import (Tk , StringVar , Text)
    from tkinter.constants import (RAISED , FLAT , BOTH , CENTER , RIDGE , END)
    from http.server import (BaseHTTPRequestHandler , HTTPServer)

except:
    raise Exception


class WebServerCreationClass:
    def __init__(self) -> str:
        self.root = Tk()
        self.root.title("WebServer Creator")
        self.root.geometry("{0}x{1}".format(350 , 550))
        self.root.resizable(0 , 0)
        self.root.config(bg = CONST_COLORS[0][0] , cursor = None)
        self.tabDashboard = Notebook(master = self.root)
        self.firstTab = Frame(self.tabDashboard)
        self.secondTab = Frame(self.tabDashboard)
        self.outputTab = Frame(self.tabDashboard)
        self.svEntryPort = StringVar(self.firstTab)
        self.svOptionChs = StringVar(self.firstTab)
        self.flaskServer = Flask(__name__)
        self.defaultHost : str = "localhost"
        self.defaultPort : int = 8080
        self.getTime = strftime("%H:%M:%S %p")
        self.optionChoices : List[str] = [
            "Python http Server" ,
            "Django Server" ,
            "Flask Server" ,
            "Nginx" ,
            "Ngrok" ,
            "Default (localhost)"
        ]
        self.svEntryPort.set(self.defaultPort)
        self.svOptionChs.set(self.optionChoices[2])

        @self.flaskServer.route('/')
        def ClickEventButton():
            newText = self.textToTemplate.get(1.0 , END)
            return f"{newText}"

        def ButtonEventClicker():
            ClickEventButton()
            try:
                self.flaskServer.debug = True
                self.flaskServer.run(
                    host = "0.0.0.0" ,
                    port = 8080
                )
            except Exception:
                pass


        self.tabDashboard.add(
            child = self.firstTab ,
            text = "Config"
        )

        self.tabDashboard.add(
            child = self.secondTab ,
            text = "Mode"
        )

        self.tabDashboard.add(
            child = self.outputTab ,
            text = "Output/Terminal"
        )

        self.firstTabLabel = Label(
            master = self.firstTab ,
            text = None ,
            background = CONST_COLORS[0][0] ,
            width = 82
        )

        self.labelPort = Label(
            master = self.firstTab ,
            text = "Port :" ,
            background = CONST_COLORS[0][0] ,
            foreground = CONST_COLORS[0][1] ,
            font = ("Vani" , 22 , font.BOLD) ,
            relief = FLAT
        )

        self.labelServer = Label(
            master = self.firstTab ,
            text = "Server :" ,
            background = CONST_COLORS[0][0] ,
            foreground = CONST_COLORS[0][1] ,
            font = ("Vani" , 22 , font.BOLD) ,
            relief = FLAT
        )

        self.secondTabLabel = Label(
            master = self.secondTab ,
            text = None ,
            background = CONST_COLORS[0][0] ,
            width = 82
        )

        self.outputtabLabel = Label(
            master = self.outputTab ,
            text = None ,
            background = CONST_COLORS[0][0] ,
            width = 82
        )

        self.entryPort = Entry(
            master = self.firstTab ,
            textvariable = self.svEntryPort ,
            background = CONST_COLORS[0][1] ,
            foreground = CONST_COLORS[0][0] ,
            font = ("Vani" , 13 , font.BOLD) ,
            justify = CENTER ,
            width = 25
        )

        self.optionSelect = Combobox(
            master = self.firstTab ,
            textvariable = self.svOptionChs ,
            values = self.optionChoices ,
            font = ("Vani" , 13 , font.BOLD) ,
            justify = CENTER
        )

        self.htmlLabel = Label(
            master = self.firstTab ,
            text = "HTML" ,
            background = CONST_COLORS[0][0] ,
            foreground = CONST_COLORS[0][1] ,
            font = ("Vani" , 22 , font.BOLD) ,
            relief = RIDGE ,
            borderwidth = 8 ,
            width = 12 ,
            justify = CENTER
        )

        self.textToTemplate = Text(
            master = self.firstTab ,
            width = 36
        )

        self.outputTerminal = Text(
            master = self.outputTab ,
            width = 43 , 
            font = ("Courier" , 15 , font.BOLD) ,
            background = CONST_COLORS[0][3] ,
            foreground = CONST_COLORS[0][2]
        )

        self.outputTerminal.insert(1.0 , f"Date : {date.today()}\nTime : {self.getTime}")

        self.createServerButton = Button(
            master = self.firstTab ,
            text = "Create Server" ,
            command = ButtonEventClicker ,
            bg = CONST_COLORS[0][4] ,
            fg = CONST_COLORS[0][1] ,
            font = ("Vani" , 15 , font.BOLD) ,
            relief = RAISED ,
            bd = 5
        )

        self.tabDashboard.pack(
            expand = 1 ,
            fill = BOTH
        )

        self.firstTabLabel.place(
            x = 0 ,
            y = 0 ,
            height = 600
        )

        self.secondTabLabel.place(
            x = 0 ,
            y = 0 ,
            height = 600
        )

        self.outputtabLabel.place(
            x = 0 ,
            y = 0 ,
            height = 600
        )

        self.labelPort.place(
            x = 0 ,
            y = 0
        )

        self.labelServer.place(
            x = 0 ,
            y = 60
        )

        self.entryPort.place(
            x = 90 ,
            y = 8
        )

        self.optionSelect.place(
            x = 120 ,
            y = 68
        )

        self.createServerButton.place(
            x = 100 ,
            y = 450
        )

        self.htmlLabel.place(
            x = 78 ,
            y = 110
        )

        self.textToTemplate.place(
            x = 25 ,
            y = 160 ,
            height = 275
        )

        self.outputTerminal.place(
            x = 0 ,
            y = 0 ,
            height = 524
        )

        self.htmlLabel.configure(anchor = CENTER)

        self.root.mainloop()



# if (platform.system()[0].upper() == "W"): 
#     WebServerCreationClass()
# else:
#     sys.exit(0)

if __name__ == "__main__":
    WebServerCreationClass()

i to consts.py

from typing import (Dict , List)
from builtins import (int , str)


CONST_COLORS : List[Dict[int , str]] = [
    {
        0 : "#1D2934" ,
        1 : "#ffffff" ,
        2 : "#00C300" ,
        3 : "#000000" ,
        4 : "#33485B"
    }
]
flask python tkinter
2021-11-23 21:23:58
1

Najlepsza odpowiedź

0

Trzeba uruchomić cykl serwera Flask w podrzędnej strumieniu i zainstalować use_reloader = False:

try
    ...
    from threading import Thread
    ...
except Exception as ex:
    print(ex)
    exit(0)

class WebServerCreationClass:
    def __init__(self):
        ...

        @self.flaskServer.route('/')
        def ClickEventButton():
            newText = self.textToTemplate.get(1.0 , END)
            return f"{newText}"

        def FlaskHandler():
            try:
                self.flaskServer.debug = True
                self.flaskServer.run(
                    host = "0.0.0.0" ,
                    port = 8080,
                    use_reloader = False
                )
            except Exception as ex:
                print(ex)

        def ButtonEventClicker():
            Thread(target=FlaskHandler, daemon=True).start()

        ...
2021-11-24 14:21:07

W innych językach

Ta strona jest w innych językach

Русский
..................................................................................................................
Italiano
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................