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:Unable to bind and unbind function with a button shallanq 2 92 Mar-28-2020, 02:05 AM
Last Post: joe_momma
  Using Tkinter widgets on child window chewy1418 8 365 Feb-27-2020, 10:34 PM
Last Post: Marbelous
  Returning a value from a tkinter.button call markr0804 4 212 Feb-16-2020, 10:35 AM
Last Post: markr0804
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 914 Jan-23-2020, 09:00 PM
Last Post: HBH
  Problem with Submit button Tkinter Reldaing 2 201 Jan-05-2020, 01:58 AM
Last Post: balenaucigasa
  [Tkinter] Mouse click event not working on multiple tkinter window evrydaywannabe 2 415 Dec-16-2019, 04:47 AM
Last Post: woooee
  [Tkinter] Window geometry appears different on Win and Linux steve_shambles 6 440 Nov-29-2019, 12:30 AM
Last Post: steve_shambles
  tkinter button not accessing the command when clicked jhf2 1 288 Nov-23-2019, 10:17 PM
Last Post: DT2000
  Tkinter button in for Simpsunt 3 247 Nov-23-2019, 01:41 AM
Last Post: Larz60+
  tkinter window and turtle window error 1885 3 583 Nov-02-2019, 12:18 PM
Last Post: 1885

Forum Jump:


Users browsing this thread: 1 Guest(s)