shrir's Profile

1564
Points

Questions
12

Answers
20

  • Asked on November 28, 2018 in tkinter.

    Here the answer:

        The problem that I needed to get the last selected item in a listbox with selectmode=MULTIPLE. In case someone else has the same problem, here is what I did:

    lastselectionList = []
    def onselect(evt):
        # Note here that Tkinter passes an event object to onselect()
        global lastselectionList
        w = evt.widget
        if lastselectionList: #if not empty
        #compare last selectionlist with new list and extract the difference
            changedSelection = set(lastselectionList).symmetric_difference(set(w.curselection()))
            lastselectionList = w.curselection()
        else:
        #if empty, assign current selection
            lastselectionList = w.curselection()
            changedSelection = w.curselection()
        #changedSelection should always be a set with only one entry, therefore we can convert it to a lst and extract                                                                                                                                                                                                                         first entry
        index = int(list(changedSelection)[0])
        value = w.get(index)
        tkinter.messagebox.showinfo("You selected ", value)
    listbox = tk.Listbox(frame,selectmode=tk.MULTIPLE)
    listbox.bind('<<ListboxSelect>>', onselect)
    listbox.pack()
    
    • 834 views
    • 5 answers
    • 0 votes
  • Asked on November 28, 2018 in tkinter.

    The answer is:

        I Add a Tkinter StringVar to your Entry widget. Bind your callback to the StringVar using the trace method.

    from Tkinter import *
     
    def callback(sv):
        print sv.get()
     
    root = Tk()
    sv = StringVar()
    sv.trace("w", lambda name, index, mode, sv=sv: callback(sv))
    e = Entry(root, textvariable=sv)
    e.pack()
    root.mainloop()
    
    • 1720 views
    • 4 answers
    • 0 votes
  • Asked on November 27, 2018 in tkinter.

    The answer:

        I believe that  you are creating thousands of objects. If you’re looking for an easy way to delete a bunch of them at once, use canvas tags http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/canvas-tags.html. This lets you perform the same operation such as deletion on a large number of objects.

    • 1486 views
    • 6 answers
    • 0 votes
  • Asked on November 27, 2018 in tkinter.

    You can try this  answer:

    See how it works for the code:

    from tkinter import *
    from PIL import ImageTk, Image
    import os
     
    root = Tk()
    img = ImageTk.PhotoImage(Image.open("True1.gif"))
    panel = Label(root, image = img)
    panel.pack(side = "bottom", fill = "both", expand = "yes")
    root.mainloop()
    
    • 584 views
    • 5 answers
    • 0 votes
  • Asked on November 27, 2018 in tkinter.

    Here is the solution is:

    import tkinter as tk
     
        def center_window(width=300, height=200):
            # get screen width and height
            screen_width = root.winfo_screenwidth()
            screen_height = root.winfo_screenheight()
     
            # calculate position x and y coordinates
            x = (screen_width/2) - (width/2)
            y = (screen_height/2) - (height/2)
            root.geometry('%dx%d+%d+%d' % (width, height, x, y))
     
    root = tk.Tk()
    center_window(500, 400)
    root.mainloop()
    
    • 523 views
    • 5 answers
    • 0 votes
  • Asked on November 27, 2018 in tkinter.

    You can try this answer:

    def logging_in(self):
    print("hi")
    user_get = self.user_entry.get()#Retrieve Username
    pass_get = self.pass_entry.get()#Retrieve Password
    

    use self.user_entry.get()

    When you are calling class variables, you need to call it by self.variable_name

    • 496 views
    • 3 answers
    • 0 votes
  • Asked on November 24, 2018 in tkinter.

    Here is the answer:

        The advantage to setting your top level object to inherit from Tk instead of Frame. The advantage arises when you have dynamic element to your GUI, e.g. a Label whose contents you want to set with a textvariable=foo instead of text= ‘Label text’.

    In this case, it is very helpful to use the Tkinter.DoubleVar, Tkinter.IntVar, and Tkinter.StringVar objects to hold the data, since the GUI will automatically update whenever these objects are set. However, to use these objects, you must specify their master as the root Tkinter.Tk() instance running. This is easier if you explicitly make your main object be a subclass of Tkinter.Tk, then have that generate frames and widgets, so you can pass along the Tk instance and set up your variables properly.

    Here is the  example for  program to illustrate the idea.

    import Tkinter as tk
     
    class Tkclass(tk.Tk):
        def __init__(self):
            tk.Tk.__init__(self)
            app=Application(self)
            app.master.title("Animal to Meat")
            app.mainloop()
     
    class Application(tk.Frame):
     
        def __init__(self, parent):
            tk.Frame.__init__(self, parent)
            self.grid(sticky=tk.N+tk.S+tk.E+tk.W)
            self.meatvar = tk.StringVar(master=parent)
            self.meatvar.set("Meat?")
            self.createWidgets()
        def createWidgets(self):
            top=self.winfo_toplevel()
            top.rowconfigure(0, weight=1)
            top.columnconfigure(0, weight=1)
            self.rowconfigure(0, weight=1)
            self.columnconfigure(0, weight=1)
            self.columnconfigure(1, weight=1)
            self.columnconfigure(2, weight=1)
            self.columnconfigure(3, weight=1)
     
            self.cowButton = tk.Button(self, text='Cow', command=self.setBeef)
            self.cowButton.grid(row=0,column=0)
            self.pigButton = tk.Button(self, text='Pig',command=self.setPork)
            self.pigButton.grid(row=0,column=1)
            self.meatLabel = tk.Label(self)
            self.meatLabel.configure(textvariable=self.meatvar)
            self.meatLabel.grid(row=0,column=2)
            self.quit = tk.Button(self, text='Quit',command=self.QuitApp)
            self.quit.grid(row=0, column=3)
     
        def setBeef(self):
            self.meatvar.set("Beef")
     
        def setPork(self):
            self.meatvar.set("Pork")
     
        def QuitApp(self):
            top=self.winfo_toplevel()
            top.quit()
     
    main = Tkclass()
    
    • 617 views
    • 4 answers
    • 0 votes
  • Asked on November 23, 2018 in tkinter.

    Try this solution:

         Anyone still looking, I have got this working as Tab in tkinter. Play around with the code to make it function the way you want (for example, you can add button to add a new tab):

    from tkinter import *
     
    class Tabs(Frame):
     
        """Tabs for testgen output"""
        def __init__(self, parent):
            super(Tabs, self).__init__()
            self.parent = parent
            self.columnconfigure(10, weight=1)
            self.rowconfigure(3, weight=1)
            self.curtab = None
            self.tabs = {}
            self.addTab()
            self.pack(fill=BOTH, expand=1, padx=5, pady=5)
     
        def addTab(self):
            tabslen = len(self.tabs)
                if tabslen < 10:
                tab = {}
                btn = Button(self, text="Tab "+str(tabslen), command=lambda: self.raiseTab(tabslen))
                btn.grid(row=0, column=tabslen, sticky=W+E)
     
                textbox = Text(self.parent)
                textbox.grid(row=1, column=0, columnspan=10, rowspan=2, sticky=W+E+N+S, in_=self)
                # Y axis scroll bar
                scrollby = Scrollbar(self, command=textbox.yview)
                scrollby.grid(row=7, column=5, rowspan=2, columnspan=1, sticky=N+S+E)
                textbox['yscrollcommand'] = scrollby.set
                tab['id']=tabslen
                tab['btn']=btn
                tab['txtbx']=textbox
                self.tabs[tabslen] = tab
                self.raiseTab(tabslen)
        def raiseTab(self, tabid):
            print(tabid)
            print("curtab"+str(self.curtab))
            if self.curtab!= None and self.curtab != tabid and len(self.tabs)>1:
                self.tabs[tabid]['txtbx'].lift(self)
                self.tabs[self.curtab]['txtbx'].lower(self)
        self.curtab = tabid
     
    def main():
        root = Tk()
        root.geometry("600x450+300+300")
        t = Tabs(root)
        t.addTab()
        root.mainloop()
     
    if __name__ == '__main__':
        main()
    
    • 816 views
    • 6 answers
    • 0 votes
  • Try this answer:

         I produced a minimal version of input validation. The following code will put up an Entry box and only accept numeric digits.

    from tkinter import *
     
    root = Tk()
     
    def testVal(inStr,acttyp):
        if acttyp == '1': #insert
            if not inStr.isdigit():
                return False
            return True
     
    entry = Entry(root, validate="key")
    entry['validatecommand'] = (entry.register(testVal),'%P','%d')
    entry.pack()
     
    root.mainloop()
    

    Perhaps I should add that I am still learning Python and I will gladly accept any and all comments/suggestions.

    • 1514 views
    • 4 answers
    • 0 votes
  • Asked on November 22, 2018 in tkinter.

    Try this solution:

        The capitalization of Tkinter and tkinter widget, method and option names is significantly different across the board. In some cases, the names themselves are different. Some features of Tkinter do not exist in tkinter, and vice-versa. But, as already stated, the main difference is that Tkinter is a module in Python 2x while tkinter is a module in Python 3x.

    • 656 views
    • 4 answers
    • 0 votes