Python,问题获取带有按钮的价值形式组合框

时间:2019-04-25 06:52:43

标签: python button tkinter combobox

我尝试通过按钮获取当前组合框的值并显示消息。 但是我做错了正确选择所选值的方法。

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

我如何获得组合框值?

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()

2 个答案:

答案 0 :(得分:0)

Entry对象和所有其他小部件的网格,打包和放置功能不返回。在python中,当您执行a()。b()时,表达式的结果就是b()返回的值,因此Entry(...)。grid(...)将返回None。

您应该将其分成两行

    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()

答案 1 :(得分:-1)

尝试,我做了一些更改

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()