UI problem in first project
I didn‘t programm for a long time now, and just started with Python last week, because I missed programming and wanted to try something new.
Now after planning my first project, I decided to start with a simple UI, and thought it should be done fairly quick.
This is what my created UI-file looks like:
And this is my script:
import ui data1 = ['Item_A', 'Item_B', 'Item_C', 'Item_D'] data2 = ['Item_E', 'Item_F'] def button1_pressed(button1): print('Button1 pressed.') def loc_selection(segmentedcontrol1): if button2.selected_index == 0: print('Segment A') data = data1 view['tableview1'].data_source.items = data elif button2.selected_index == 1: print('Segment B') data = data2 view['tableview1'].data_source.items = data def list_item_clicked(tableview1): print('Item selected.') view = ui.load_view('test_01') view.name = 'Test 01' data = data1 view['tableview1'].data_source.items = data button2 = view['segmentedcontrol1'] loc_selection('segmentedcontrol1') view.present('sheet')
It’s a really simple thing probably, but it really was a challenge for me to get this done so far :D
My problem now: I want to run a specific function if a list item is selected and I can‘t find a way how to do it.
I would be really greatful for any help!
cvp last edited by cvp
@Af305 If I correctly understand, put
button2.action = loc_selection
@Af305 and, more usual
def loc_selection(sender): if sender.selected_index == 0: print('Segment A') data = data1 elif sender.selected_index == 1: print('Segment B') data = data2 view['tableview1'].data_source.items = data```
@cvp thank you for your help!
Regarding your first message:
I‘m calling the loc_selection function here to get that print(„Segment A“). I want to replace print with a boolean later on to say „Segment A“ selected = True.
Thanks for your advice in your second message! Didn‘t think about that.
Unfortunately I don‘t understand your third message - thats exactly how it‘s set in my UI file.
What i want to do with my TableView is:
If Item_A selected -> function_a()
If Item_B selected -> function_b()
If Item_C selected -> function_c()
If Item_D selected -> function_d()
If Item_E selected -> function_e()
If Item_F selected -> function_f()
Currently I‘ve set „list_item_clicked“ as action for my TableView. Thats working so far, but I don‘t know how to differentiate between the rows to call a different function for each of them. Right now my whole TableView is acting like one fat Button with just a single action.
@Af305 remove the action in your ui designer and the def in the script, and add this
class MyTableViewDelegate(object): def tableview_did_select(self, tableview, section, row): print(row) view['tableview1'].delegate = MyTableViewDelegate()
You will have to read a bit about delegate
@cvp that‘s exactly what I was looking for! Thank you very much for your help!
I‘ll read about it.