Python Forum
[Tkinter] Tkinter window pop up again when i click button
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.
[Image: ss.png]
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()
Reply
#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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 489 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 977 Jan-16-2024, 06:44 PM
Last Post: rob101
  tkinter - touchscreen, push the button like click the mouse John64 5 841 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Tkinter multiple windows in the same window tomro91 1 845 Oct-30-2023, 02:59 PM
Last Post: Larz60+
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,717 May-25-2023, 07:37 PM
Last Post: deanhystad
  Figure Gets Larger Every time I click a show button joshuagreineder 2 1,326 Aug-11-2022, 06:25 AM
Last Post: chinky
  [Tkinter] Open tkinter colorchooser at toplevel (so I can select/focus on either window) tabreturn 4 1,900 Jul-06-2022, 01:03 PM
Last Post: deanhystad
  [Tkinter] Background inactivity timer when tkinter window is not active DBox 4 2,922 Apr-16-2022, 04:04 PM
Last Post: DBox
  [Tkinter] Clicking on the button crashes the TK window ODOshmockenberg 1 2,238 Mar-10-2022, 05:18 PM
Last Post: deanhystad
  why my list changes to a string as I move to another window in tkinter? pymn 4 2,576 Feb-17-2022, 07:02 AM
Last Post: pymn

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020