Python Forum
[Tkinter] Displaying ComboBoxDialog Widget in a Class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Displaying ComboBoxDialog Widget in a Class
#1
from tkinter import *
import Pmw

class Combobox:
    def __init__(self):
        self.root = Tk()
        self.root.title('ComboBoxDialog')
        self.listed = ['Toyota','Honda','Mercerdise Benz','BMW','KIA','Ford','Peoguet','Nissan','Opel']
        # self.build_combo()
        self.root.mainloop()
        # self.choose = None

    def build_combo(self):
        self.choose = Pmw.ComboBoxDialog(self.root,title='ComboBox',buttons=('Ok','Cancel'),
        defaultbutton = 'Ok',combobox_labelpos=N,label_text='Which Car?',listbox_width=22,
        scrolledlist_items=self.listed,selectioncommand=self.click)
        # self.choose.pack(side=TOP,padx=5,pady=5,expand=YES,fill=X)
        self.tkraise()
    def click(self, args):
        print(f'You choose {args}')
        self.choose.configure(label_text=args)

if __name__ ==  '__main__':
    Combobox()
Hi! Am trying to adapt coding in a 'class' since like almost all programmers uses it. But the issue here is that the 'frame' and title pops up but the content (ComboBoxDialog Widget) does not.
Please help me out? Thanks.
Reply
#2
from tkinter import *
import Pmw
 
class Combobox:
    def __init__(self):
        self.root = Tk()
        self.root.title('ComboBoxDialog')
        # self.root.withdraw()
        self.listed = ['Toyota','Honda','Mercerdise Benz','BMW','KIA','Ford','Peoguet','Nissan','Opel']
        self.build_combo()
        self.root.mainloop()
        # self.choose = None
 
    def build_combo(self):
        # self.choose = Pmw.ComboBoxDialog(self.root, title='ComboBox', buttons=('Ok', 'Cancel'),
        #     defaultbutton = 'Ok', combobox_labelpos=N, label_text='Which Car?', listbox_width=22,
        #     scrolledlist_items=self.listed, selectioncommand=self.click)
        self.choose = Pmw.ComboBoxDialog(
            self.root, 
            title='ComboBox', 
            buttons=('Ok', 'Cancel'),
            defaultbutton = 'Ok',
            combobox_labelpos=N,
            label_text='Which Car?',
            listbox_width=22,
            scrolledlist_items=self.listed)
            # selectioncommand=self.click)

        self.choose.pack(side=TOP,padx=5,pady=5,expand=YES,fill=X)
        # self.tkraise()

    def click(self, args):
        print(f'You choose {args}')
        self.choose.configure(label_text=args)
 
if __name__ ==  '__main__':
    Combobox()
The normal to do this type of thing is using ttk.combobox.
I am unfamiliar with Pmw combo box, so not sure how it wants list elements presented, but it was not happy with your method.
You can remove the root window with root.withdraw(), but then you need to intercept the normal exit (using x) so that you can destroy the withdrawn root window, otherwise there will be a tkinter zombie still running and you will have to delete it manually.
if you don't have any special reason for running Pmw, I'd replace with tkinter.ttk.
Reply
#3
(Feb-02-2019, 02:28 AM)Larz60+ Wrote:
 from tkinter import * import Pmw class Combobox: def __init__(self): self.root = Tk() self.root.title('ComboBoxDialog') # self.root.withdraw() self.listed = ['Toyota','Honda','Mercerdise Benz','BMW','KIA','Ford','Peoguet','Nissan','Opel'] self.build_combo() self.root.mainloop() # self.choose = None def build_combo(self): # self.choose = Pmw.ComboBoxDialog(self.root, title='ComboBox', buttons=('Ok', 'Cancel'), # defaultbutton = 'Ok', combobox_labelpos=N, label_text='Which Car?', listbox_width=22, # scrolledlist_items=self.listed, selectioncommand=self.click) self.choose = Pmw.ComboBoxDialog( self.root, title='ComboBox', buttons=('Ok', 'Cancel'), defaultbutton = 'Ok', combobox_labelpos=N, label_text='Which Car?', listbox_width=22, scrolledlist_items=self.listed) # selectioncommand=self.click) self.choose.pack(side=TOP,padx=5,pady=5,expand=YES,fill=X) # self.tkraise() def click(self, args): print(f'You choose {args}') self.choose.configure(label_text=args) if __name__ == '__main__': Combobox() 
The normal to do this type of thing is using ttk.combobox. I am unfamiliar with Pmw combo box, so not sure how it wants list elements presented, but it was not happy with your method. You can remove the root window with root.withdraw(), but then you need to intercept the normal exit (using x) so that you can destroy the withdrawn root window, otherwise there will be a tkinter zombie still running and you will have to delete it manually. if you don't have any special reason for running Pmw, I'd replace with tkinter.ttk.
(Feb-02-2019, 02:28 AM)Larz60+ Wrote:
 from tkinter import * import Pmw class Combobox: def __init__(self): self.root = Tk() self.root.title('ComboBoxDialog') # self.root.withdraw() self.listed = ['Toyota','Honda','Mercerdise Benz','BMW','KIA','Ford','Peoguet','Nissan','Opel'] self.build_combo() self.root.mainloop() # self.choose = None def build_combo(self): # self.choose = Pmw.ComboBoxDialog(self.root, title='ComboBox', buttons=('Ok', 'Cancel'), # defaultbutton = 'Ok', combobox_labelpos=N, label_text='Which Car?', listbox_width=22, # scrolledlist_items=self.listed, selectioncommand=self.click) self.choose = Pmw.ComboBoxDialog( self.root, title='ComboBox', buttons=('Ok', 'Cancel'), defaultbutton = 'Ok', combobox_labelpos=N, label_text='Which Car?', listbox_width=22, scrolledlist_items=self.listed) # selectioncommand=self.click) self.choose.pack(side=TOP,padx=5,pady=5,expand=YES,fill=X) # self.tkraise() def click(self, args): print(f'You choose {args}') self.choose.configure(label_text=args) if __name__ == '__main__': Combobox() 
The normal to do this type of thing is using ttk.combobox. I am unfamiliar with Pmw combo box, so not sure how it wants list elements presented, but it was not happy with your method. You can remove the root window with root.withdraw(), but then you need to intercept the normal exit (using x) so that you can destroy the withdrawn root window, otherwise there will be a tkinter zombie still running and you will have to delete it manually. if you don't have any special reason for running Pmw, I'd replace with tkinter.ttk.
Please replace with tkinter.ttk let's see. And please help me with tkinter.ttk tutorials because I want to understand it very well. Again, between Pmw and ttk which is better? Thanks.
Reply
#4
Quote:Please replace with tkinter.ttk let's see.
This is for you to do.

Quote:Again, between Pmw and ttk which is better?
Since I'm not completely familiar with Pmw, I can't say.
Pmw has been around for a long time, so I'm confident that it's probably OK.
However ttk is known by a many more programmers, so help is easier to come by. Your choice.

Quote:And please help me with tkinter.ttk tutorials because I want to understand it very well.
google: 'tkinter.ttk tutorials' there are many.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] remove widget from other class issac_n 2 3,068 Aug-05-2020, 01:55 PM
Last Post: deanhystad
  How to place global tk text widget in class or on canvas puje 1 2,284 Jul-04-2020, 09:25 AM
Last Post: deanhystad
  [Tkinter] Class with text widget method AeranicusCascadia 3 7,739 Nov-14-2017, 11:33 PM
Last Post: AeranicusCascadia

Forum Jump:

User Panel Messages

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