I'm trying to put together this GUI, it's my first attempt. It's mostly a collage of code I put together from samples I found here and there.
So, far so good, except I can't seem to be able to figure out how to pass "text variables" from an array to labels.
#If you go to def printLabels(sel f), and def on_move(), you will see what I mean:),
Thanks a million. (I'm just learnig to #program, sorry about the mess!)
So, far so good, except I can't seem to be able to figure out how to pass "text variables" from an array to labels.
#If you go to def printLabels(sel f), and def on_move(), you will see what I mean:),
Thanks a million. (I'm just learnig to #program, sorry about the mess!)
Code:
#! /usr/bin/env python # -*- coding: cp932 -*- from Tkinter import * import tkMessageBox #If you go to def printLabels(self) you will see my #question, thanks a million. (I'm just learnig to #program, sorry about the mess!) class GUIFramework(Frame): """This is the GUI""" def __init__(self,master=None): """Initialize yourself""" """Initialise the base class""" Frame.__init__(self,master) """Set the Window Title""" self.master.title("Parts-Weight Counting Application") self.master.geometry("820x410+60+50") #--------the radio buttons seect the language to be used on my labels #But I can't figure out how to pass the elements of the selected array to #my labels! """Display the main window with a little bit of padding""" self.spanish=["Seleccione la pieza a contar",\ "Proporcione el peso del contenedor/bolsa",\ "Contar piezas","OK"] self.english=["Select produt you want to count",\ "Provide weight of container/bag",\ "Count pieces","OK"] self.japanese=[u"数えたい部品名を選択してください",\ u"容器または袋の重さを記入してください",\ u"清算開始",u"OK"] self.grid(padx=20, pady=20,sticky=N+S+E+W) self.CreateWidgets() def CreateWidgets(self): """Create all the widgets that we need""" """Create the Text""" self.top=Frame(self) self.top.grid(row=0, column=0) self.frmLang=Frame(self.top)#language frame self.frmLang.grid(row=0, column=0)#listbox frame self.frmLbx=Frame(self.top) self.frmLbx.grid(row=1, column=0,columnspan=2,sticky=N+S+E+W) self.frmDlgBoxes=Frame(self.top) self.frmDlgBoxes.grid(row=1, column=3,columnspan=2,sticky=(N, W, E, S)) self.frmDlgBoxes2=Frame(self.frmDlgBoxes) self.frmDlgBoxes2.grid(row=3, column=5,columnspan=2,sticky=(N, W, E, S)) self.listBoxLb = Label(self.frmLbx, text="Select part name:") self.listBoxLb.grid(row=3, column=0, sticky=W) #----------------------------------------------------------------------- self.labelTotalWeigt = Label(self.frmDlgBoxes,\ text="Enter total weight of parts:") self.labelTotalWeigt.grid(row=1, column=1, sticky=E) #create labels self.labelLngg = Label(self.frmLang, text='---', bg='yellow') #label dislaying array contents languages self.labelLngg.grid(row=0, column=4,columnspan=2, pady=5) #-------------------------------------------------------------------------- #create radiobtns self.rb_v = IntVar() self.partWeight=float(.1000000)#IntVar() self.lbParts=StringVar() self.selection=StringVar() self.label_text=StringVar() self.result=list self.id_rb_english = 101 self.rb_english = Radiobutton(self.frmLang, text='Englsih',\ value=self.id_rb_english, \ variable=self.rb_v, command=self.on_click) self.rb_english.grid(row=0, column=0) self.id_rb_spanish = 102 self.rb_spanish = Radiobutton(self.frmLang, text='Spanish',\ value=self.id_rb_spanish, \ variable=self.rb_v, command=self.on_click) self.rb_spanish.grid(row=0, column=1) self.id_rb_japanese = 103 self.rb_japanese = Radiobutton(self.frmLang, text=u'日本語',\ value=self.id_rb_japanese, \ variable=self.rb_v, command=self.on_click) self.rb_japanese.grid(row=0, column=2) """Create the ListBox""" scrollbarV = Scrollbar(self.frmLbx, orient=VERTICAL) #scrollbarH = Scrollbar(self, orient=HORIZONTAL) #Create Listbox self.lbParts = Listbox(self.frmLbx, selectmode=BROWSE , yscrollcommand=scrollbarV.set) #self.lbSites2 = Listbox(self.frmLang, selectmode=BROWSE # , yscrollcommand=scrollbarV.set) #self.text=self.on_move() scrollbarV.grid(row=1, column=4, sticky=N+S) scrollbarV.config(command=self.lbParts.yview) self.lbParts.grid(row=1, column=0, columnspan=4, sticky=N+W+S+E) #self.lbSites2.grid(row=5, column=0, columnspan=4, sticky=N+W+S+E) """Just fill up the listbox with some numbers""" for i in range(50): self.lbParts.insert(END, "Part No. %s"%i) """Create the Add, Remove, Edit, and View Buttons""" self.btnAdd = Button(self.frmLbx, text="Add") self.btnAdd.grid(column=0, row=2, stick=E, pady=5) self.btnRemove = Button(self.frmLbx, text="Remove") self.btnRemove.grid(column=1, row=2, stick=E, pady=5) self.btnEdit = Button(self.frmLbx, text="Edit") self.btnEdit.grid(column=2, row=2, stick=E, pady=5) self.btnView = Button(self.frmLbx, text="View",command=self.printLabels) self.btnView.grid(column=3, row=2, stick=E, pady=5) #create Entrybox self.totalWeightParts = StringVar() self.countedParts = StringVar() self.container = StringVar() self.container_entry = Entry(self.frmDlgBoxes, width=7,\ textvariable=self.totalWeightParts) self.container_entry.grid(column=3, row=1, sticky=E) self.weight_entry = Entry(self.frmDlgBoxes, width=7,\ textvariable=self.container) self.weight_entry.grid(column=3, row=0, sticky=E) self.btnCnt=Button(self.frmDlgBoxes, text="Count", command=self.calculate) self.btnCnt.grid(column=3, row=8, sticky=E) #create entry box listbox # use entry widget to display/edit selection self.enter1 = Entry(self.frmLbx, width=40, bg='blue',foreground="white") self.enter1.insert(0, 'Select item') self.enter1.grid(row=3, column=1) # pressing the return key will update edited line ##self.enter1.bind('<Return>', set_list) # or double click left mouse button to update line ##self.enter1.bind('<Double-1>', set_list) #label results dialog self.LabelShowPtsCtd=Label(self.frmDlgBoxes,\ textvariable=self.countedParts,\ bg='yellow').grid(column=3, row=7, sticky=(W, E)) self.Labelmgs=Label(self.frmDlgBoxes, text="Kg/mg").grid(column=4, row=0, sticky=W) self.Labelmgs2=Label(self.frmDlgBoxes, text="Kg/mg").grid(column=4, row=1, sticky=W) self.LabelCntnrWgt=Label(self.frmDlgBoxes,\ text="Enter container weight:").grid(column=1, row=0, sticky=E) self.LabelTxtPtsCntd5=Label(self.frmDlgBoxes, \ text="parts counted").grid(column=1, row=7, sticky=E) for child in self.frmDlgBoxes.winfo_children(): child.grid_configure(padx=5, pady=5) self.container_entry.focus() self.weight_entry.focus() self.frmDlgBoxes.bind('<Return>', self.calculate) self.lbParts.bind('<ButtonRelease-1>', self.get_list) def get_list(self,*args): """ function to read the listbox selection(s) (mutliple lines can be selected) and put the result(s) in a label """ # tuple of line index(es) self.selection = self.lbParts.curselection()[0] self.seltext = self.lbParts.get(self.selection) # delete previous text in enter1 self.enter1.delete(0, 50) # now display the selected text self.enter1.insert(0, self.seltext) #self.label_text.set(self.seltext) #print self.seltext,self.selection def calculate(self): try: self.value = float(self.totalWeightParts.get()) print self.value print self.weight_entry.get() print self.partWeight self.countedParts.set((self.value - float(self.weight_entry.get()))\ /self.partWeight) except ValueError: pass def on_click(self): """radio button clicked, change results too""" self.on_move() def on_move(self,value=0): if self.rb_v.get() == self.id_rb_english: self.result = self.english #print self.result[0] elif self.rb_v.get() == self.id_rb_spanish: self.result = self.spanish #print self.result[0] else: self.result = self.japanese #print self.result[0] #--------------------------------------------------------------------------- #print self.result #this are the tags I want to use for my labels. self.labelLngg['text'] =self.result[0]#How I can pass the value to this #label, but It won't let me do the same other labels? eg. label below: #self.LabelCntnrWgt['text'] =self.result[1] #--------------------------------------------------------------------------- def printLabels(self): #how do I pass these values to my labels? please m(_._)m #To see printed results, select language and press the "View" button print self.result[0] print self.result[1] print self.result[2] print self.result[3] #pass if __name__ == "__main__": guiFrame = GUIFramework() guiFrame.mainloop()
Comment