Tkinter button remains sunken when pressed

2019-06-02 06:12发布

I don't know exactly where my problem lies, it is either in the bindings, or its the function being called itself.

My buttons all appear under a labelFrame and each are accompanied with an entry box, so that many csv files can be loaded by the user for analysis, and the filepaths are visible. It has to be searched by the user as these files can existed in my different and unconnected project folders.

This is an example button.

csv_type21 = Button(csvfiles, text = "Browse ...")
csv_type21.bind("<Button-1>", lambda event, obj = var21, tid = 21: find_cvsfile(event, obj, tid))

They remain sunken if the user cancels looking for the file, and even if they choose a file it stays sunken. (but the entry box does get updated with the filepath).

This is the function they call, it asks for a csv file and saves it as a tuple, so that I can keep track of which csv is which. After that I set the StringVar for the Entry box that the button is in line with to state the filepath.

def find_csvfile(event, obj, tid):
input_csvfile = askopenfile(initialdir = 'C:/',
                            filetypes = [("CSV File", "*.csv")],
                            title = 'Open CSV File for id ' + str(tid) + '...',
                            mode = 'r')
csv_data = tid, input_csvfile
filepath = input_csvfile.name
obj.set(filepath)

I've tried fiddling loads of different variables and arguments but nothing works. I think it has to be the multiple buttons calling the same command but I can't be sure.

Any help is appreciated.

Thanks.

3条回答
闹够了就滚
2楼-- · 2019-06-02 06:30

Probe using button.bind("",funcion)

def funcion()
   print

when use "" call start when mouse button down and release during the funcion() execution.

when return the state is pressed

查看更多
做个烂人
3楼-- · 2019-06-02 06:33

Add return "break" at the end of def find_csvfile(event, obj, tid):

查看更多
甜甜的少女心
4楼-- · 2019-06-02 06:38

Solved the problem by removing the binding. The lambda command was moved into the Button declaration and the "event" parameter removed from the find_csvfile function. This solution stops the buttons being sunken when pressed.

csv_type21 = Button(csvfiles, text = "Browse ...", command = lambda obj = var21, tid = 21: find_csvfile(obj, tid))
查看更多
登录 后发表回答