0

I try to get the value of the current combobox with a button and display the message. But i do something wrong to take correctly the value selected.

AttributeError: 'SelectDB' object has no attribute 'cmb'

How i do to get the combobox value ?

import sqlite3
from tkinter import *
from tkinter import ttk
from tkinter import messagebox

class SelectDB:
    def __init__(self, wind) :
        self.wind = wind
        self.wind.title ('MyApp')
        ttk.Label(self.wind, text="Select you're Database:").grid (row = 0, column = 0)
        ttk.Combobox(self.wind, width="10", values=("Local (sqllite)","MYSQL")).grid (row = 0, column = 1)
        ttk.Button(text="Start", command=self.checkcmbo).grid (row = 1, column = 0)

    def checkcmbo(self):
        if self.cmb.get() == "Local (sqllite)":
            messagebox.showinfo("What user choose", "you choose Local (sqllite)")
        elif self.cmb.get(self) == "MYSQL":
            messagebox.showinfo("What user choose", "you choose MYSQL")
        else:
            messagebox.showinfo("What user choose", "NOTHING")

if __name__ == '__main__':
    wind = Tk()
    application = SelectDB(wind)
    wind.mainloop()
Manada
  • 61
  • 9
  • 1
    The error is self-explanatory - you never define `cmb`. – Henry Yik Apr 25 '19 at 07:00
  • I try cmb=ttk.Button(text="Start", command=self.checkcmbo).grid (row = 1, column = 0) but it's the same. How i do cmd ? What i do to take correctly ? – Manada Apr 25 '19 at 07:25
  • self.cmb = ttk.Combobox(self.wind, width="10", values=("Local (sqllite)","MYSQL")).grid (row = 0, column = 1) – ncica Apr 25 '19 at 07:37
  • 1
    Read [Tkinter: AttributeError: NoneType object has no attribute get](https://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-get) – Henry Yik Apr 25 '19 at 07:38
  • @ncica that will result in an `AttributeError`. – Henry Yik Apr 25 '19 at 07:38

2 Answers2

0

The grid, pack and place functions of the Entry object and of all other widgets returns None. In python when you do a().b(), the result of the expression is whatever b() returns, therefore Entry(...).grid(...) will return None.

You should split that on to two lines

    self.cmb = ttk.Combobox(self.wind, width="10", values=("Local (sqllite)","MYSQL"))
    self.cmb.grid (row = 0, column = 1)
class SelectDB:
def __init__(self, wind) :
    self.wind = wind
    self.wind.title ('MyApp')

    ttk.Label(self.wind, text="Select you're Database:").grid (row = 0, column = 0)

    self.cmb = ttk.Combobox(self.wind, width="10", values=("Local (sqllite)","MYSQL"))
    self.cmb.grid (row = 0, column = 1)
    btn = ttk.Button(text="Start", command=self.checkcmbo).grid (row = 1, column = 0)

def checkcmbo(self):
    if self.cmb.get() == "Local (sqllite)":
        messagebox.showinfo("What user choose", "you choose Local (sqllite)")
    elif self.cmb.get() == "MYSQL":
        messagebox.showinfo("What user choose", "you choose MYSQL")
    else:
        messagebox.showinfo("What user choose", "NOTHING")

  if __name__ == '__main__':
      wind = Tk()
      application = SelectDB(wind)
      wind.mainloop()
ncica
  • 7,015
  • 1
  • 15
  • 37
  • 1
    Many thanks. I understand better because it failed. And it will serve as an example in the future. I will remember self.cmb + self.cmb.grid + btn........... – Manada Apr 25 '19 at 08:20
-1

try so, I've make some changes

import sqlite3
from tkinter import *
from tkinter import ttk
from tkinter import messagebox

class SelectDB:
    def __init__(self, wind) :
        self.wind = wind
        self.wind.title ('MyApp')
        ttk.Label(self.wind, text="Select you're Database:").grid (row = 0, column = 0)
        self.cbCombo = ttk.Combobox(self.wind, width="10", values=("Local (sqllite)","MYSQL"))
        self.cbCombo.grid (row = 0, column = 1)
        ttk.Button(text="Start", command=self.checkcmbo).grid (row = 1, column = 0)

    def checkcmbo(self):

        if self.cbCombo.current()!=-1:
            if self.cbCombo.current() ==0:
                msg="You choos sqlite"
            else:
                msg="You choos MYSQL"

            messagebox.showwarning('MyApp',msg,)
        else:
            messagebox.showwarning('MyApp','You must choose something!',)

if __name__ == '__main__':
    wind = Tk()
    application = SelectDB(wind)
    wind.mainloop()
1966bc
  • 1,148
  • 1
  • 6
  • 11
  • Thank you. It also serves as an example. Thank you. – Manada Apr 25 '19 at 08:21
  • Your answer would be more useful if you explained the changes. Otherwise we have to compare your code to the original line by line and character by character to see what you did to fix the problem. – Bryan Oakley Apr 25 '19 at 13:35