python - Tkinter Toplevel in OOP script: how? -


goal of script:

  • (3) different windows, each in own class, own widgets , layout, created via toplevel , callbacks.
  • when new (toplevel) window created, previous 1 destroyed. thus, 1 window visible , active @ time.

problem? basically, i've tried many things , failed, must understand little of ["parent", "master", "root", "app", "..."] :(

note on raising windows: have implemented successful example of loading frames on top of each other, , controlling visibility via .raise method.

for problem, however, don't want load frames @ once. abstracted version of quiz program require quite lot of frames images, makes me reluctant load @ once.

script (not working; bugged):

#!/usr/bin/env python  tkinter import * import tkmessagebox, tkfont, random, ttk  class first_window(frame):     """the option menu shown @ startup"""     def __init__(self, master):         frame.__init__(self, master)          self.gotosecond = button(text = "start", command = self.goto_second)         self.gotosecond.grid(row = 2, column = 3, sticky = w+e)      def goto_second(self):         self.master.withdraw()         self.master.update_idletasks()         second_window = toplevel(self)  class second_window(toplevel):     """the gamewindow questions, timer , entrywidget"""     def __init__(self, *args):         toplevel.__init__(self)         self.focus_set()          self.gotothird = button(text = "gameover", command = self.goto_third)         self.gotothird.grid(row = 2, column = 3, sticky = w+e)      def goto_third(self):         third_window = toplevel(self)         self.destroy()  class third_window(toplevel):     """highscores shown buttons startmenu"""     def __init__(self, *args):         toplevel.__init__(self)         self.focus_set()         self.master = first_window          self.gotofirst = button(text = "startover", command = self.goto_first)         self.gotofirst.grid(row = 2, column = 3, sticky = w+e)      def goto_first(self):         self.master.update()         self.master.deiconify()         self.destroy()  def main():     root = tk()     root.title("algebra game pjk")     app = first_window(root)     root.resizable(false,false)     app.mainloop()  main() 

the problem not tkinter problem, basic problem classes vs. instances. actually, 2 similar separate problems. need read through tutorial on classes, the 1 in official python tutorial.


first:

self.master = first_window 

first_window class. have instance of class (in global variable named app), represents first window on screen. can call update , deiconify , forth on instance, because represents window. first_window isn't representing particular window, it's class, factory creating instances represent particular windows. can't call update or deiconify on class.

what want pass first window down through chain of windows. (you could, alternatively, access global, or various other things, seems cleanest.) you're trying pass second_window, need stash , pass again in second_window (instead of passing self instance, useless—it's destroyed window object), , stash , use in third_window.


second:

second_window = toplevel(self) 

instead of creating instance of second_window class, you're creating instance of generic toplevel class, , giving local name second_window (which temporarily hides class name… but since never use class, doesn't matter).

and have same problem when try create third window.


so:

class first_window(frame):     # ...     def goto_second(self):         # ...         second = second_window(self)  class second_window(toplevel):     def __init__(self, first, *args):         toplevel.__init__(self)         self.first = first         # ...      def goto_third(self):         third = third_window(self.first)         self.destroy()  class third_window(toplevel):     """highscores shown buttons startmenu"""     def __init__(self, first, *args):         toplevel.__init__(self)         self.first = first         # ...      def goto_first(self):         self.first.update()         self.first.deiconify()         self.destroy() 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -