0

I need to pass MainWindow instance to DashboardWindow object. So DashboardWindow can change apparently as i want. the event trigger when login button press. Or u have other to implement flow tkinter switch window in event.

import tkinter

class MainWindow(tkinter.Tk):
    _title = 'Login'
    _wWidth = 640
    _wHeight = 360
    _resizable = False

    def __init__(self):
        super().__init__()

        self.resizable(self._resizable, self._resizable)
        self.minsize(self._wWidth, self._wHeight)
        self.title(self._title)
        self.geometry('+0+0')
        self.iconbitmap('./project/images/discord.ico')
    
        click = tkinter.Button(self, text= 'Login', command= DashboardWindow(self))
        click.pack(side= tkinter.TOP)
    

class DashboardWindow:
    def __init__(self):
        self._title = 'Dashboard'
        self._wWidth = 1024
        self._wHeight = 600
        self._resizable = True
    
mainWindow = MainWindow()
mainWindow.mainloop()
david stephen
  • 329
  • 4
  • 12
  • 1
    Use `command=lambda: DashboardWindow(self)` – TheLizzard Aug 17 '21 at 13:18
  • 1
    Does this answer your question? [How to pass arguments to a Button command in Tkinter?](https://stackoverflow.com/questions/6920302/how-to-pass-arguments-to-a-button-command-in-tkinter) – TheLizzard Aug 17 '21 at 13:18
  • You should not be using an instance of DashboardWindow as the callback. Whatever you are trying to do that is not right. A more usual pattern is to make a dashboard object an attribute of MainWindow. Not sure if that works for you because I am not exactly sure where you are going. – Andrew Allaire Aug 17 '21 at 13:21
  • @AndrewAllaire can u show code? i basically need make gui like usual. mainwindow ( login ) when success. it creates new window for dashboard. but, mainwindow disapparead, and so on. – david stephen Aug 17 '21 at 13:24
  • @AndrewAllaire why not, what is wrong with doing so? did you expect for example that it would be a frame and then callback packs the frame or sth? Otherwise if you have a separate window then why not call the instance? I understand the frame thing, because then you can choose the layout manager and stuff but otherwise I don't see anything wrong with this – Matiiss Aug 17 '21 at 13:29
  • Are you going to need the login window to come back at some time, or are you done with it after login and the rest of the application session is just the new dashboard window? – Andrew Allaire Aug 17 '21 at 13:29
  • @Matiiss The expression DashboardWindow(self) will of course fail in this case since the DashboardWindow.__init__ takes no additional arguments other than self atm. But presuming he fixed this and had it take a MainWindow object as an argument, then the thing returned would be a DashboardWindow object, which is not callable so not soemthing that should be passed to command, which must be a callable. – Andrew Allaire Aug 17 '21 at 13:33
  • @AndrewAllaire oh, you were talking about the current OP code, I thought you took into account what @TheLizzard said that OP should change it to `comand=lambda: ...` which would work because it would be actually calling the class on button press not using the returned value from constructor (would be using the returned value of lambda or sth) – Matiiss Aug 17 '21 at 13:38
  • @AndrewAllaire i need LoginWindow again, when specific event occurs like trial have finished or membership is expired. thats why i need to create seperate class for each window. – david stephen Aug 17 '21 at 15:17
  • the `command` shouldn't be calling a class. It should call a function. Using a proper function will make the code easier to write and easier to debug. – Bryan Oakley Aug 17 '21 at 15:30
  • @AndrewAllaire i think class is like page in html. each layout html have each configuration that show specific layout. – david stephen Aug 17 '21 at 15:30
  • Ok, well Tkinter has Top level objects (see https://www.tutorialspoint.com/python/tk_toplevel.htm). For which you can bring up entirely new windows, you can also hide them. Within a top level object a relatively independent area with its own geometry management containing other widgets is a Frame. – Andrew Allaire Aug 17 '21 at 15:46
  • Also, don't think like html, tkinter is different. There are several reasoanble approaches to your problem, my own preference would be to make subclasses of Frame for your two window types. Initialize one of each and put them each inside their own top level window object. They should have attributes that reference the other one, and methods that will deiconify themself and bring up the other, and these methods should be passed to the command args in button widgets. Note, pass the actual method, do not call the method and then return the result to the command option. – Andrew Allaire Aug 17 '21 at 15:58

0 Answers0