MySQL查询未在Tkinter Label Widget上显示

时间:2016-07-05 03:05:29

标签: python mysql python-2.7 tkinter

背景

通过使用Tkinter,我设法列出了MySQL表中的部件号。我可以从列表中选择一个项目作为变量并打印它但是,无法打印输出以下的MySQL。我的代码如下:

代码:

#!/usr/bin/env python

from ttk import Frame, Label
from Tkinter import Tk, BOTH, Listbox, StringVar, END
import MySQLdb

class Example(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)   
        self.parent = parent
        self.initUI()


    def initUI(self):

        self.parent.title("My Parts List")          

        self.pack(fill=BOTH, expand=1)

        db = MySQLdb.connect("localhost", "root", "password", "my_database")
        cursor=db.cursor()
        cursor.execute("SELECT part_number FROM stock_lists ORDER BY part_number ASC")

        lb = Listbox(self)

        for reading in cursor.fetchall():
            string_length=len(reading)
            cut_from_end= int(string_length) - 4
            lb.insert(END, str(reading)[2:cut_from_end])

        lb.bind("<<ListboxSelect>>", self.onSelect)    

        lb.pack(pady=15)

        self.var = StringVar()
        self.label1 = Label(self, text=0, textvariable=self.var)
        myvar=str(self.var)
        #http://stackoverflow.com/questions/775296/python-mysql-parameterized-queries
        cursor.execute("SELECT part_name FROM stock_lists WHERE part_number = %s", (myvar))
        self.myvar=StringVar()
        self.myvar.set(cursor.fetchone())
        self.label2 = Label(self, text=0, textvariable=self.myvar)
        cursor.execute("SELECT qty_on_hand FROM stock_lists WHERE part_number = %s", (myvar))

        self.myvar.set(cursor.fetchone())
        self.label3 = Label(self, text=0, textvariable=self.myvar)
        cursor.execute("SELECT bin FROM stock_lists WHERE part_number = %s", (myvar))

        self.myvar.set(cursor.fetchone())
        self.label4 = Label(self, text=0, textvariable=self.myvar) 
        self.label1.pack()
        self.label2.pack()
        self.label3.pack()
        self.label4.pack()

    def onSelect(self, val):

        sender = val.widget
        idx = sender.curselection()
        value = sender.get(idx)   

        self.var.set(value)


def main():

    root = Tk()
    ex = Example(root)
    root.geometry("300x250+300+300")
    root.mainloop()  


if __name__ == '__main__':
    main()

输出:

我已将图片编辑为&#34;滚动条&#34;红色。 enter image description here

问题:

Q1:如何在上面的代码中将滚动条放在列表旁边?

Q2:为什么Label ~2~3~4显示为&#34;无&#34;?< / p>

问题3:如何在这些标签中显示字符串,以便输出显示:

enter image description here

0 个答案:

没有答案