I want to get the value of the combo box placed in the Tkinter sub-window, but I get an error.

Asked 2 years ago, Updated 2 years ago, 90 views

I just started programming.
You can display windows and combo boxes, but when you select items in the combo box, you get an error.
Below is the bit device type acquisition part.
I've tried many things, but it doesn't work.Thank you for your cooperation.

import tkinter ask
import tkinter.ttk as ttk
import sys
from tkinter.filedialog import askopenfilename 
from tkinter import*

class mainwindow(tk.Frame):
    def__init__(self, master=None):
        super().__init__(master)
        self.master.title("TEST")# Window Title Settings
        self.master.geometry("600x600")# window size setting

        self.pack()#Mainframe Placement
        self.create_menu()#Generate Menu
    
    # Generating a Menu
    def create_menu(self):

        # open a configuration window
        def winsetting():
            settingwin=Toplevel(self)
            settingwin.title ("SUB")
            settingwin.geometry("300x300")
            # Set to modal (until this window closes, no other window operations are allowed)
            settingwin.grab_set()

            # arrangement of parts
            label_bittype=tk.Label(settingwin, text="bit device Type")
            label_bittytype.place(x=5,y=70)
            # Bit Device Type
            bit_type=ttk.Combobox(settingwin, values=("A", "B", "C", "D", "E"), state="readonly")
            bit_type.bind("<<ComboboxSelected>>", get_bititem)
            bit_type.place(x=100,y=70)

        # Get Bit Device Type <----------------------------------------------------------------
        def get_bititem(self, event):
            print(bit_type.get())
    
        self.menubar=tk.Menu(self)#Generate a menubar instance from the Menu class
        root.config(menu=self.menubar)# Menu Bar Placement
 
        self.setting_menu=tk.Menu(self.menubar,tearoff=0)#Generate large item "Settings" in menu bar
        self.menubar.add_cascade(label="settings", menu=self.setting_menu)# Install the big item "settings"
        self.setting_menu.add_command(label="settings", command=winsetting)# Assign subitem "settings"

if__name__=="__main__":
    root=tk.Tk()
    app=mainwindow(master=root)
    app.mainloop()

python python3 tkinter

2022-09-30 10:28

1 Answers

First of all, there are two types of fixes, and the ones I commented on in ### are the fixes.
I can't say which is better, and there may be a better way.

In any case, it would be better to clarify where variables and functions/methods are defined/affiliated, and where they can be accessed.

Note: Simplettk ComboBox demo

  • def get_bititem(self,event):Move from def create_menu(self): to class mainwindow(tk.Frame):.
  • bit_type.bind("<<ComboboxSelected>>",get_bititem) does not have enough self. for callback routine specification.
  • Change to self.get_bititem.
  • def winsetting(): or def get_bititem(self, event): bit_type is missing before bit_type.Change to self.bit_type respectively.
import tkinter ask
import tkinter.ttk as ttk
import sys
from tkinter.filedialog import askopenfilename 
from tkinter import*

class mainwindow(tk.Frame):
    def__init__(self, master=None):
        super().__init__(master)
        self.master.title("TEST")# Window Title Settings
        self.master.geometry("600x600")# window size setting
        self.pack()#Mainframe Placement
        self.create_menu()#Generate Menu

    #### Move Definition Position
    # Get Bit Device Type
    def get_bititem(self, event):
        Add print(self.bit_type.get())####self.

    # Generating a Menu
    def create_menu(self):
        # open a configuration window
        def winsetting():
            settingwin=Toplevel(self)
            settingwin.title ("SUB")
            settingwin.geometry("300x300")
            # Set to modal (until this window closes, no other window operations are allowed)
            settingwin.grab_set()

            # arrangement of parts
            label_bittype=tk.Label(settingwin, text="bit device Type")
            label_bittytype.place(x=5,y=70)
            # Bit Device Type
            add self.bit_type=ttk.Combobox(settingwin, values=("A", "B", "C", "D", "E"), state="readonly")###self.
            Added self.bit_type.bind("<<ComboboxSelected>>",self.get_bititem)####self.
            Added self.bit_type.place(x=100,y=70)####self.

        self.menubar=tk.Menu(self)#Generate a menubar instance from the Menu class
        root.config(menu=self.menubar)# Menu Bar Placement

        self.setting_menu=tk.Menu(self.menubar,tearoff=0)#Generate large item "Settings" in menu bar
        self.menubar.add_cascade(label="settings", menu=self.setting_menu)# Install the big item "settings"
        self.setting_menu.add_command(label="settings", command=winsetting)# Assign subitem "settings"

if__name__=="__main__":
    root=tk.Tk()
    app=mainwindow(master=root)
    app.mainloop()

Note: Tkinter Tutorial - Combo Box Combo Box

  • defget_bititem(self,event): self, is unnecessary. event is the best defget_bititem(event): Define
  • def get_bititem(event): in def create_menu(self):, not in def create_menu(self): but in def winsetting(): bit_type.bind("<<ComboboxSelected>">>>>>>bitem> before .
import tkinter ask
import tkinter.ttk as ttk
import sys
from tkinter.filedialog import askopenfilename 
from tkinter import*

class mainwindow(tk.Frame):
    def__init__(self, master=None):
        super().__init__(master)
        self.master.title("TEST")# Window Title Settings
        self.master.geometry("600x600")# window size setting
        self.pack()#Mainframe Placement
        self.create_menu()#Generate Menu

    # Generating a Menu
    def create_menu(self):
        # open a configuration window
        def winsetting():
            settingwin=Toplevel(self)
            settingwin.title ("SUB")
            settingwin.geometry("300x300")
            # Set to modal (until this window closes, no other window operations are allowed)
            settingwin.grab_set()

            #### Move Definition Position
            # Get Bit Device Type
            def get_bititem(event):deleting ####self,
                print(bit_type.get())

            # arrangement of parts
            label_bittype=tk.Label(settingwin, text="bit device Type")
            label_bittytype.place(x=5,y=70)
            # Bit Device Type
            bit_type=ttk.Combobox(settingwin, values=("A", "B", "C", "D", "E"), state="readonly")
            bit_type.bind("<<ComboboxSelected>>", get_bititem)
            bit_type.place(x=100,y=70)

        self.menubar=tk.Menu(self)#Generate a menubar instance from the Menu class
        root.config(menu=self.menubar)# Menu Bar Placement

        self.setting_menu=tk.Menu(self.menubar,tearoff=0)#Generate large item "Settings" in menu bar
        self.menubar.add_cascade(label="settings", menu=self.setting_menu)# Install the big item "settings"
        self.setting_menu.add_command(label="settings", command=winsetting)# Assign subitem "settings"

if__name__=="__main__":
    root=tk.Tk()
    app=mainwindow(master=root)
    app.mainloop()


2022-09-30 10:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.