python - Tkinter gui mainloop "ex =" -


hello i'm complete beginner in python , facing problem while creating simple gui in tkinter (trying make simple pong). anyways have code in python 3:

    tkinter import frame import tkinter  class pong(frame):      def __init__(self, parent):         frame.__init__(self, parent)            self.parent = parent         self.parent.bind("<key>", self.key)         self.initialize()      def key(self, event):         if event.char == 'q':  #end             self.quit()         print("end")     def initialize(self):         print("initialize")         pass  def main():      root = tkinter.tk()     ex = pong(root)     root.overrideredirect(true)     root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))     root.mainloop()  main() 

i'm wondering bottom "ex = pong(root)" part because if delete program doesn't work, cant press q quit, can't find ex means, literally can't find documentation online.

the pong class wrapper frame. represents gui application, , encapsulates (hides away) tkinter gui stuff.

a frame "window" content shown in. root tk master frame. root base window - destroying root destroys (closes) gui.

with in mind, recommend trying following changes:

class pong(frame):  ...      def key(self, event):         if event.char == 'q':  #end             # close base frame, , our application frame             self.parent.quit()             self.quit()      # wrapper methods root control     def overrideredirect( bool ):         self.parent.overrideredirect(bool)      def geometry(geo_string):         self.parent.geometry(geo_string)      def mainloop():         self.parent.mainloop()  ...  def main():      root = tkinter.tk()     wdth, hght = root.winfo_screenwidth(), root.winfo_screenheight()     ex = pong(root)     ex.overrideredirect(true)     ex.geometry("{0}x{1}+0+0".format(wdth, hght))     ex.mainloop() 

you can drive master window using pong. used approach before , found safer - didn't confuse root frame class , make nasty accidents! safer subclassing tk.

also add method pong, generate_gui(), , code setup gui widgets. safer setting widgets main function directly.

check out these in meantime:

http://effbot.org/tkinterbook/

http://www.tkdocs.com/tutorial/


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -