2

Background

By using Tkinter I have managed to list part numbers from a MySQL table. I am able to select an item as a variable from the list and print it but, unable to print the output the following MySQL. My code is below:

Code:

#!/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()

Output:

I have edited the image to have "scrollbar" in red. enter image description here

Questions:

Q1: How can I place a Scrollbar next the list in the code above?

Q2: Why is Labels ~2, ~3, and ~4displaying as "None"?

Q3: How can I display a strings in these labels so that output looks:

enter image description here

3kstc
  • 1,871
  • 3
  • 29
  • 53

0 Answers0