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.