Python Forum
I'm a new Python programmer and I need some tips
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I'm a new Python programmer and I need some tips
#1
I toke an online Python course for beginners the course was good and there was an final project on the first section, so I tried to Do it by myself by applying what I have learned, I did do it but there was no GUI on my programm so when I tried to improve my programm by adding GUI I stuck I didnt know how to link the code to the frame 

I want to know if there is a standard to build the program GUI on any program 

Like where is the best place to creacte the main window root = Tk() and the main loop 

And shold I create multiple windows or one is enough 

And so on 

Thanks
Reply
#2
Without seeing your program I don't think we can say. Each program is different so some might need many frames and others none and so on. If you show the program you couldn't get to work I expect advice can be give.
Reply
#3
Hi Qubayel

Here a very simple GUI Tk Template:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from functools import partial

try:
    # Tkinter for Python 2.xx
    import Tkinter as tk
    import tkFont as fnt
except ImportError:
    # Tkinter for Python 3.xx
    import tkinter as tk
    import tkinter.font as fnt

APP_TITLE = "Simple GUI Tk Template"
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 300
APP_HEIGHT = 200


class Application(tk.Frame):

    def __init__(self, master):
        self.master = master
        self.master.protocol("WM_DELETE_WINDOW", self.close)
        tk.Frame.__init__(self, master)

    def close(self):
        print("Application-Shutdown")
        self.master.destroy()
    
def main():
    app_win = tk.Tk()
    app_win.title(APP_TITLE)
    app_win.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))
    app_win.geometry("{}x{}".format(APP_WIDTH, APP_HEIGHT))
    
    app = Application(app_win).pack(fill='both', expand=True)
    
    app_win.mainloop()
 
 
if __name__ == '__main__':
    main()      
wuf Smile
Reply
#4
(Mar-31-2017, 07:18 PM)wuf Wrote: Hi Qubayel

Here a very simple GUI Tk Template:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from functools import partial

try:
    # Tkinter for Python 2.xx
    import Tkinter as tk
    import tkFont as fnt
except ImportError:
    # Tkinter for Python 3.xx
    import tkinter as tk
    import tkinter.font as fnt

APP_TITLE = "Simple GUI Tk Template"
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 300
APP_HEIGHT = 200


class Application(tk.Frame):

    def __init__(self, master):
        self.master = master
        self.master.protocol("WM_DELETE_WINDOW", self.close)
        tk.Frame.__init__(self, master)

    def close(self):
        print("Application-Shutdown")
        self.master.destroy()
    
def main():
    app_win = tk.Tk()
    app_win.title(APP_TITLE)
    app_win.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))
    app_win.geometry("{}x{}".format(APP_WIDTH, APP_HEIGHT))
    
    app = Application(app_win).pack(fill='both', expand=True)
    
    app_win.mainloop()
 
 
if __name__ == '__main__':
    main()      
wuf Smile

thanks for your response 
can you please explain what dose these lines of code do? 
def __init__(self, master):
   self.master = master
   self.master.protocol("WM_DELETE_WINDOW", self.close)
   tk.Frame.__init__(self, master)
Moderator Larz60+: Added Python tags. Please do this in the future (see help, BBCODE)
Reply
#5
Here's the remplate I use which works for both applications with (or without a parent window)

#!/usr/bin/env python
try:
    # Tkinter for Python 2.xx
    import Tkinter as tk
except ImportError:
    # Tkinter for Python 3.xx
    import tkinter as tk


class GuiTemplate(tk.Frame):
    def __init__(self, parent=None, title='Template Title',
                 geometry='600x400+10+10'):
        if parent:
            self.parent = parent
        else:
            self.parent = tk.Tk()
        self.parent.title(title)
        self.parent.geometry(geometry)
        self.parent.protocol("WM_DELETE_WINDOW", self.quit)
        tk.Frame.__init__(self, parent)

        self.parent.mainloop()

    def quit(self):
        self.parent.destroy()
     

def main():
    GuiTemplate()

  
if __name__ == '__main__':
    main()
I added wuf's quit routine
Reply
#6
(Mar-31-2017, 08:17 AM)Barrowman Wrote: Without seeing your program I don't think we can say. Each program is different so some might need many frames and others none and so on. If you show the program you couldn't get to work I expect advice can be give.

thanks for your reply, this is the code I wrote so far (The Python will be attached)
I want to display a login window when the admin name and password are correct destroy the window and create a new one to continue to menus 
but I dont know where should i create the root window to be the main window after the login window is destroyed

Attached Files

.py   aaaaaa.py (Size: 3.84 KB / Downloads: 206)
Reply
#7
Hi Qubayel

self.master.protocol("WM_DELETE_WINDOW", self.close)
If you click the X-Button in the title bar of the main window this line of code shall call the 'close'-method where you can place code to be executed before the final close of the main window.

tk.Frame.__init__(self, master)
This shall create a main frame in the main window in which you place further application widgets.

wuf Smile
Reply


Forum Jump:

User Panel Messages

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