Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [Tkinter] Tkinter window pop up again when i click button
#1
Hi guys, im new at python and new at programming code.
i got confused when run a little program that i created on.
so it pop up again when i click "run service", did i miss something ? thx.
İmage

source code : https://kuronekosan.web.id/source.txt

from tkinter import *
# from Outdated_Versions import server as outdated_Server



class HttpServer:

    def __init__(self,root):
        self.LabelProject = Label(root, text="HTTP Server Project Using Python")
        self.LabelProject2 = Label(root, text="Kelompok 2 - HTTP")
        self.label_1 = Label(root, text="IP Address / HOST")
        self.label_2 = Label(root, text="PORT")

        # self.entryIP = StringVar()
        # self.entryIP = IntVar()

        self.entryIP = Entry(root)
        self.entryPORT = Entry(root)

        # self.EntryIP_Str =
        # self.EntryPORT_Int =
        #
        # Normally, the characters that the
        # user types appear in the entry.To
        # make a.password.entry that echoes
        # each character as an asterisk, set
        # show = "*".
        #
        self.chkFrame = BooleanVar()
        self.FrameSettingsModule = Checkbutton(root, var=self.chkFrame )
        self.labelFrameSettingsModule = Label(root, text="Use FLASK Framework")
        self.chkDefault = BooleanVar()
        self.defaultSettingsModule = Checkbutton(root, var=self.chkDefault, command=self.cacheDefault )
        self.labeldefaultSettingsModule = Label(root, text="Use Default Settings")
        self.chkSave = BooleanVar()
        self.saveSettingsModule = Checkbutton(root, var=self.chkSave)
        self.labelsaveSettingsModule = Label(root, text="Save Settings")
        self.button_Process = Button(root, text="Run Service", fg="green", command=self.ButtonPress )
        self.button_Exit = Button(root, text="Stop Service", fg="red")


        self.LabelProject.grid(row=0, columnspan=2)
        self.LabelProject2.grid(row=1, columnspan=2)
        self.label_1.grid(row=2, sticky=W)
        self.label_2.grid(row=3, sticky=W)
        self.entryIP.grid(row=2, column=1)
        self.entryPORT.grid(row=3, column=1)
        self.FrameSettingsModule.grid(row=4)
        self.labelFrameSettingsModule.grid(row=4, column=1)
        self.defaultSettingsModule.grid(row=5)
        self.labeldefaultSettingsModule.grid(row=5, column=1)
        self.saveSettingsModule.grid(row=6)
        self.labelsaveSettingsModule.grid(row=6, column=1)
        self.button_Exit.grid(row=7, column=1)
        self.button_Process.grid(row=7)

    def cacheDefault(self):
        self.varCheckDefault = self.chkDefault.get()
        self.varCheckFrame = self.chkFrame.get()

        if self.varCheckDefault == True:
            self.entryIP.delete(0, END)
            self.entryIP.insert(0, "localhost")
            self.entryIP.config(state='disabled')
            self.entryPORT.delete(0, END)
            self.entryPORT.insert(0, 80)
            return
        else:
            self.entryIP.config(state='normal')
            self.entryIP.delete(0, END)
            self.entryPORT.delete(0, END)

    def ButtonPress(self):
        self.varCheckDefault = self.chkDefault.get()
        self.varCheckFrame = self.chkFrame.get()
        IP = self.entryIP.get()
        PORT = self.entryPORT.get()
        if self.varCheckFrame == True:
            if IP == 'localhost':
                IP = '127.0.0.1'
            self.Framework(IP,PORT)
        else:
            if self.varCheckDefault == True:
                # self.callServer(PORT)
                pass
            else:
                print("You need Checklist the Default Settings with the Old Version!")




    def callServer(self,PORT):
        from http.server import HTTPServer, BaseHTTPRequestHandler
        class Server(BaseHTTPRequestHandler):
            def do_GET(self):
                if self.path == '/':
                    self.path = '/index.html'
                try:
                    file_to_open = open(self.path[1:]).read()
                    self.send_response(200)
                except:
                    file_to_open = "File not found"
                    self.send_response(404)
                self.end_headers()
                self.wfile.write(bytes(file_to_open, 'utf-8'))
        httpd = HTTPServer(('',PORT), Server)
        httpd.serve_forever()

    def Framework(self,IP,PORT):
        IP = self.entryIP.get()
        PORT = self.entryPORT.get()
        try:
            from flask import Flask
            app = Flask(__name__)
            @app.route("/")
            def main():
                return "Welcome to my Flask page!"
            if __name__ == "__main__":
                app.run(debug=True, host=IP, port=PORT)
        except ModuleNotFoundError:
            print('Your Flask Framework is not installed!, please install it with "pip3 install flask"')
        except:
            print('Error! please check the code and connection!')

master = Tk()
frame = Frame(master, width=125, height=35)
master.resizable(False, False)
master.title("HTTP Server")

frame.grid()


callInit = HttpServer(master)
master.mainloop()
buran wrote May-09-2019, 04:35 PM:
I post your code here in python tags, change title to meaningful one and moved the thread to correct sub-forum
Quote
#2
worked for me with the callServer function commented.
I'm using linux, 3.5 idle
Did you run from console? or pycharm only?
I did notice a problem, when you get the port entry widget it's returning a string and port needs to be converted to integer. (int(PORT)) then the web server threw an error asking for permission.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  tkinter window and turtle window error 1885 3 156 Nov-02-2019, 12:18 PM
Last Post: 1885
  [Tkinter] Checking button click in Tkinter GalaxyCoyote 3 185 Oct-20-2019, 03:28 AM
Last Post: GalaxyCoyote
  [Tkinter] Tkinter button help and general commands Iceman 1 194 Sep-01-2019, 05:09 PM
Last Post: Yoriz
  [Tkinter] Extrakt a Variable from a closed tkinter window hWp 5 284 Aug-23-2019, 09:01 PM
Last Post: woooee
  [PyQt] Problem how to click a button inside a group box? mart79 2 212 Aug-05-2019, 01:21 PM
Last Post: mart79
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 360 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] Not getting entry values on button click & treeview not updating ? swanysto 4 1,011 May-10-2019, 04:16 PM
Last Post: swanysto
  [Tkinter] Top Level Window - from tkinter import * francisco_neves2020 6 553 Apr-23-2019, 09:27 PM
Last Post: francisco_neves2020
  [WxPython] Adding a Window to a Button wxPython ShashankDS 4 544 Apr-23-2019, 06:53 PM
Last Post: Yoriz
  tkinter- adding a new window after clicking a button built on the gui ShashankDS 2 687 Apr-18-2019, 12:48 PM
Last Post: ShashankDS

Forum Jump:


Users browsing this thread: 1 Guest(s)