Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102086 > unrolled thread

tkinter newbie question

Started byKP <kai.peters@gmail.com>
First post2016-01-24 20:19 -0800
Last post2016-01-25 06:14 -0800
Articles 5 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  tkinter newbie question KP <kai.peters@gmail.com> - 2016-01-24 20:19 -0800
    Re: tkinter newbie question Peter Otten <__peter__@web.de> - 2016-01-25 09:51 +0100
      Re: tkinter newbie question KP <kai.peters@gmail.com> - 2016-01-25 08:21 -0800
        Re: tkinter newbie question KP <kai.peters@gmail.com> - 2016-01-25 08:23 -0800
    Re: tkinter newbie question KP <kai.peters@gmail.com> - 2016-01-25 06:14 -0800

#102086 — tkinter newbie question

FromKP <kai.peters@gmail.com>
Date2016-01-24 20:19 -0800
Subjecttkinter newbie question
Message-ID<ce955291-9e43-4482-9f0c-ca3f6853b0ae@googlegroups.com>
See my code below (which works). I'd like to have the 2nd window as a class in a separate unit. How do I code that unit and how do I call it from my first unit?

As always, thanks for all help!




#!/usr/bin/env python
"""
""" 
from tkinter import *
from settings import *

class window1():

    def open_window2(self):
        t = Toplevel(self.root)
        t.title('New window')
        t.geometry('262x65+200+250')
        t.transient(self.root)

    def setup_menu(self):
        self.menubar = Menu(self.root)
        self.menu1 = Menu(self.menubar, tearoff=0 ) 
        self.menu1.add_command(label="Settings",   accelerator='Ctrl+S', command=self.open_window2)
        self.menubar.add_cascade(label="Menu 1", menu=self.menu1)  
        self.root.config(menu=self.menubar)        

    def __init__(self):
        self.root = Tk()
        self.root.title('Window #1')
        self.setup_menu()
        self.root.geometry('800x600+200+200')
        #
        self.root.mainloop()

if __name__ == '__main__':
    
    w1 = window1()

[toc] | [next] | [standalone]


#102087

FromPeter Otten <__peter__@web.de>
Date2016-01-25 09:51 +0100
Message-ID<mailman.215.1453711872.15297.python-list@python.org>
In reply to#102086
KP wrote:

> See my code below (which works). 

>From the import of lowercase "tkinter" I conclude you are using Python 3.

> I'd like to have the 2nd window as a
> class in a separate unit. How do I code that unit and how do I call it
> from my first unit?
> 
> As always, thanks for all help!

Move the code from open_window2() into a class in settings.py, e. g.


import tkinter as tk # avoid star-import

class SettingsWindow(tk.Toplevel): # Class names start with uppercase letter
                                   # Prefer self-explaining names
    def __init__(self, root):
        super().__init__(root)
        self.title('New window')
        self.geometry('262x65+200+250')
        self.transient(root)

Then use it in your main script:


> #!/usr/bin/env python
> """
> """
> from tkinter import *
import settings

> class window1():
> 
>     def open_window2(self):
          settings.SettingsWindow(self.root)

>     def setup_menu(self):
>         self.menubar = Menu(self.root)
>         self.menu1 = Menu(self.menubar, tearoff=0 )
>         self.menu1.add_command(label="Settings",   accelerator='Ctrl+S',
>         command=self.open_window2) self.menubar.add_cascade(label="Menu
>         1", menu=self.menu1) self.root.config(menu=self.menubar)
> 
>     def __init__(self):
>         self.root = Tk()
>         self.root.title('Window #1')
>         self.setup_menu()
>         self.root.geometry('800x600+200+200')
>         #
>         self.root.mainloop()
> 
> if __name__ == '__main__':
>     
>     w1 = window1()

[toc] | [prev] | [next] | [standalone]


#102093

FromKP <kai.peters@gmail.com>
Date2016-01-25 08:21 -0800
Message-ID<fe69e528-c1c8-4784-afd1-1b7df67a9b38@googlegroups.com>
In reply to#102087
On Monday, 25 January 2016 00:51:34 UTC-8, Peter Otten  wrote:
> KP wrote:
> 
> > See my code below (which works). 
> 
> >From the import of lowercase "tkinter" I conclude you are using Python 3.
> 
> > I'd like to have the 2nd window as a
> > class in a separate unit. How do I code that unit and how do I call it
> > from my first unit?
> > 
> > As always, thanks for all help!
> 
> Move the code from open_window2() into a class in settings.py, e. g.
> 
> 
> import tkinter as tk # avoid star-import
> 
> class SettingsWindow(tk.Toplevel): # Class names start with uppercase letter
>                                    # Prefer self-explaining names
>     def __init__(self, root):
>         super().__init__(root)
>         self.title('New window')
>         self.geometry('262x65+200+250')
>         self.transient(root)
> 
> Then use it in your main script:
> 
> 
> > #!/usr/bin/env python
> > """
> > """
> > from tkinter import *
> import settings
> 
> > class window1():
> > 
> >     def open_window2(self):
>           settings.SettingsWindow(self.root)
> 
> >     def setup_menu(self):
> >         self.menubar = Menu(self.root)
> >         self.menu1 = Menu(self.menubar, tearoff=0 )
> >         self.menu1.add_command(label="Settings",   accelerator='Ctrl+S',
> >         command=self.open_window2) self.menubar.add_cascade(label="Menu
> >         1", menu=self.menu1) self.root.config(menu=self.menubar)
> > 
> >     def __init__(self):
> >         self.root = Tk()
> >         self.root.title('Window #1')
> >         self.setup_menu()
> >         self.root.geometry('800x600+200+200')
> >         #
> >         self.root.mainloop()
> > 
> > if __name__ == '__main__':
> >     
> >     w1 = window1()

Dang - almost there. Using your code, I get the new window with the specified geometry and its type is transient, as expected.

Its caption, however, is NOT the caption specified, but the caption of the first window, leaving me with 2 windows with identical caption.

Any idea why?

[toc] | [prev] | [next] | [standalone]


#102094

FromKP <kai.peters@gmail.com>
Date2016-01-25 08:23 -0800
Message-ID<a8cfb796-abe2-4867-9d0b-b5ca62483e49@googlegroups.com>
In reply to#102093
On Monday, 25 January 2016 08:22:12 UTC-8, KP  wrote:
> On Monday, 25 January 2016 00:51:34 UTC-8, Peter Otten  wrote:
> > KP wrote:
> > 
> > > See my code below (which works). 
> > 
> > >From the import of lowercase "tkinter" I conclude you are using Python 3.
> > 
> > > I'd like to have the 2nd window as a
> > > class in a separate unit. How do I code that unit and how do I call it
> > > from my first unit?
> > > 
> > > As always, thanks for all help!
> > 
> > Move the code from open_window2() into a class in settings.py, e. g.
> > 
> > 
> > import tkinter as tk # avoid star-import
> > 
> > class SettingsWindow(tk.Toplevel): # Class names start with uppercase letter
> >                                    # Prefer self-explaining names
> >     def __init__(self, root):
> >         super().__init__(root)
> >         self.title('New window')
> >         self.geometry('262x65+200+250')
> >         self.transient(root)
> > 
> > Then use it in your main script:
> > 
> > 
> > > #!/usr/bin/env python
> > > """
> > > """
> > > from tkinter import *
> > import settings
> > 
> > > class window1():
> > > 
> > >     def open_window2(self):
> >           settings.SettingsWindow(self.root)
> > 
> > >     def setup_menu(self):
> > >         self.menubar = Menu(self.root)
> > >         self.menu1 = Menu(self.menubar, tearoff=0 )
> > >         self.menu1.add_command(label="Settings",   accelerator='Ctrl+S',
> > >         command=self.open_window2) self.menubar.add_cascade(label="Menu
> > >         1", menu=self.menu1) self.root.config(menu=self.menubar)
> > > 
> > >     def __init__(self):
> > >         self.root = Tk()
> > >         self.root.title('Window #1')
> > >         self.setup_menu()
> > >         self.root.geometry('800x600+200+200')
> > >         #
> > >         self.root.mainloop()
> > > 
> > > if __name__ == '__main__':
> > >     
> > >     w1 = window1()
> 
> Dang - almost there. Using your code, I get the new window with the specified geometry and its type is transient, as expected.
> 
> Its caption, however, is NOT the caption specified, but the caption of the first window, leaving me with 2 windows with identical caption.
> 
> Any idea why?

Forget that post - mea culpa - figured it out - sorry!

[toc] | [prev] | [next] | [standalone]


#102090

FromKP <kai.peters@gmail.com>
Date2016-01-25 06:14 -0800
Message-ID<090b49ef-1a1c-49bd-8adf-da51031d5b30@googlegroups.com>
In reply to#102086
On Sunday, 24 January 2016 20:20:07 UTC-8, KP  wrote:
> See my code below (which works). I'd like to have the 2nd window as a class in a separate unit. How do I code that unit and how do I call it from my first unit?
> 
> As always, thanks for all help!
> 
> 
> 
> 
> #!/usr/bin/env python
> """
> """ 
> from tkinter import *
> from settings import *
> 
> class window1():
> 
>     def open_window2(self):
>         t = Toplevel(self.root)
>         t.title('New window')
>         t.geometry('262x65+200+250')
>         t.transient(self.root)
> 
>     def setup_menu(self):
>         self.menubar = Menu(self.root)
>         self.menu1 = Menu(self.menubar, tearoff=0 ) 
>         self.menu1.add_command(label="Settings",   accelerator='Ctrl+S', command=self.open_window2)
>         self.menubar.add_cascade(label="Menu 1", menu=self.menu1)  
>         self.root.config(menu=self.menubar)        
> 
>     def __init__(self):
>         self.root = Tk()
>         self.root.title('Window #1')
>         self.setup_menu()
>         self.root.geometry('800x600+200+200')
>         #
>         self.root.mainloop()
> 
> if __name__ == '__main__':
>     
>     w1 = window1()

Thank you - much appreciated!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web