0

I am trying to get a button to do some stuff in Python (using Tkinter). I want the button to get the current choice of a combobox and print it on screen.

The graphical layout is 3 separate frames, but both the button and the combobox reside in the same frame (frame #2).

The problem is that i cannot refer to the combobox. The errors i am getting read:

Frame object has no attribute 'box'

Window object has no attribute 'box'

self.box=ttk.Combobox(self.frame2 , values[...])
self.button1=tk.Button(self.frame2, command= self.wipe(), text=...)

def wipe(self):
    self.box.get()

ALTERNATIVELY i tried:

def wipe(self):
    self.frame2.box.get()

The goal is to simply get the selected choice from the Combobox.

HERE IS MINIMAL CODING THAT PRODUCES THE SAME ERROR:

import tkinter as tk
from tkinter import ttk

class window():
    def __init__(self,root):
        self.frame=tk.Frame(root)
        self.key=tk.Button(self.frame,text='PRESS ME',command=self.wipe())
        self.box=ttk.Combobox(self.frame, options=['1','2','3'])
        self.frame.pack()
        self.key.pack()
        self.box.pack()
    def wipe(self):
        self.box.get()

master=tk.Tk()
master.geometry('400x400')
app=window(master)
master.mainloop()
Akenaten
  • 91
  • 9

1 Answers1

0

I would add the tag "tkinter" to the question.

Try the following:

def wipe(self):
    # code

self.box=ttk.Combobox(self.frame2 , values[...])
self.button1=tk.Button(self.frame2, command=wipe, text=...)

Notice the following:

  1. I first defined wipe, only then used it.
  2. I am not quite sure why you wanted to do command=self.wipe(), as there are two issues here. First, you are setting command to the result of self.wipe() which is NOT a function. Second, you haven't defined self.wipe, you defined wipe.
  3. command=wipe Sets the command keyword argument to the function wipe

Its been long since I dealt with tkinter, if this doesn't work, I'll try to help by checking the docs again.

Saad
  • 3,340
  • 2
  • 10
  • 32
Sir Donnie
  • 163
  • 7
  • Yes i did try defining the function before the widgets but it didn't change anything. I also tried phrasing the command in various other ways which also didn't change much if anything ( i mean the 'command=self.wipe()' , 'self.wipe' etc.) – Akenaten Apr 23 '19 at 16:41
  • Please edit your question to include your attempts. – Sir Donnie Apr 23 '19 at 16:45