Hello

This is my first post on this forum. I have found that this place seems to have lot of knowledgeable people when it comes to Python. Anyway I'm quite new to GUI programming and I'm not exactly having a huge problem but I'm more interested about how to make my code prettier and proper.

Issue is that I have defined this class where I have three buttons that all call the same function/subroutine/def but with each button I would like to pass different variable.. in this case a number.

I found a working solution and I have also tried to reduce the amount of code with putting the whole thing in loop.. not using lambda: self.cVid("2") but lambda: self.cVid(length) and then repeating "length" with different numbers like length = "2" , length = "3" etc. Basically everything I try besides this working code results in only the last number being passed.. doesn't matter which button I press.

I understand that it is somehow related to tkinter loop but I have no idea how to handle this situation to make the code little bit more compact and not repeat this function definition for buttons each time:

buttonContainer = lambda: self.cVid("2")

class chooseVideoFiles:
	def __init__(self):
		canv = self.root = Toplevel(root)
		canv.geometry("+150+250")
		title = Label(canv, text = "Select 3 videos with specified or a bit longer length", font = ("Helvetica", 10, "bold")).grid()
		buttonContainer = lambda: self.cVid("2")
		Button(canv, text = "2 minutes", command = buttonContainer).grid(padx = 3, pady = 5, row = 1, column = 0, sticky = W)
		Label(canv, text = "Line 1").grid(padx = 85, pady = 5, row = 1, column = 0, sticky = W)
		buttonContainer = lambda: self.cVid("3")
		Button(canv, text = "3 minutes", command = buttonContainer).grid(padx = 3, pady = 5, row = 2, column = 0, sticky = W)
		Label(canv, text = "Line 2").grid(padx = 85, pady = 5, row = 2, column = 0, sticky = W)
		buttonContainer = lambda: self.cVid("5")
		Button(canv, text = "5+ minutes", command = buttonContainer).grid(padx = 3, pady = 5, row = 3, column = 0, sticky = W)
		Label(canv, text = "Line 3").grid(padx = 85, pady = 5, row = 3, column = 0, sticky = W)
	def cVid(self,length):
		chooseVideoFile(length)

Maybe somebody who has had to deal with similar situation knows how I should put this to rest.

Recommended Answers

All 4 Replies

The first thing that sticks out like a sore thumb is that you are using tabs for indentations. It makes for ugly and troublesome code. Use the standard 4 spaces for indents like most people do.

I prefer to bind the button to a function that returns the desired variable, but it is somewhat personal preference.

from Tkinter import *

class ChooseVideoFiles:
    def __init__(self, root):
        canv = Toplevel(root)
        canv.geometry("+150+250")
        title = Label(canv, 
              text = "Select 3 videos with specified or a bit longer length",
              font = ("Helvetica", 10, "bold")).grid()
        self.buttons(canv)
        exit = Button(canv, text='Exit', command=top.quit).grid(row=5,column=0)

    ##-------------------------------------------------------------------         
    def buttons(self, canv):
        b_row=1
        b_col=0
        parameter_list = [ [2, "2 minutes", "Line 1"], \
                           [3, "3 minutes", "Line 2"], \
                           [5, "5+ minutes", "Line 3"] ]

        for param in parameter_list:
            b = Button(canv, text=param[1])
            b.grid(padx=3, pady=5, row=b_row, column=b_col, sticky=W)
            lab = Label(canv, text=param[2]).grid(padx=93, pady=5, 
                        row=b_row, column=b_col, sticky=W)

            def handler ( event, self=self, button_num=param[0] ):
                return self.cb_handler( event, button_num )
            b.bind ( "<Button-1>", handler )

            b_row += 1

    ##----------------------------------------------------------------
    def cb_handler( self, event, cb_number ):
        print "cb_handler", cb_number                

top = Tk()
CV = ChooseVideoFiles(top)
top.mainloop()

thank for your solution woooee, it is definitely something to work with although I'm still open to other alternatives too.

@vegaseat had no idea about this.. I figure Python writers are the most space key damaging programmers in the world :D

I doubt I'm able to unlearn it though.. not that hard to mass convert fortunately.

You do not need to push even tab most times as reasonable editors know Python indention style, even 4 spaces is the default tab width. Mostly I hit this isssue when I try code in IDLE command line and want to copy it over to my program file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.