Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Use scene and ui in the same program
-
Hello,
I created a scene that shows the solar system. If you choose a date (D=...) in the program, the scene shows you the position of the planets on this date. But, I would like to create an interface where you can ask the user the date he would like to see. I never used the modul ui but I saw that it could be possible thanks to this modul. Could anyone tell me how I could create this ?
Thanks a lot. -
@pythonista21 very short example (if I correctly understood your question):
import ui d = ui.DatePicker() d.name = 'Select a date' d.mode = ui.DATE_PICKER_MODE_DATE def date_action(sender): print(sender.date) d.action = date_action d.present('sheet')
-
Thank you for your quick answer! I think you correctly understood my question but there is still a problem.
I added your example at the beginning of my program, but the Date picker and the scene that shows the planet are shown at the same time, so I can't choose a date because all the windows are opened at the same time. -
@pythonista21 put this line after the d.present
d.wait_modal()
-
The date picker appeared first, it is much better. But the problem is that I did my calculations with years and not with months and days, that's why the date that is given by the date picker can't be used in my program I think, because it can't be converted into years. Do you think that I could only ask the user a year and remove the months and days ?
-
@pythonista21 not very nice but I'm not an expert 😢
import ui d = ui.DatePicker() d.name = 'Select a year' d.mode = ui.DATE_PICKER_MODE_DATE def date_action(sender): print(str(sender.date)[:4]) d.action = date_action l = ui.Button() l.frame = (0,0,d.width*2/3,d.height) l.background_color = 'white' d.background_color = 'white' d.add_subview(l) d.present('sheet') d.wait_modal()
-
import ui d = ui.DatePicker() d.mode = ui.DATE_PICKER_MODE_DATE def date_action(sender): print(str(sender.date)[:4]) sender.close() d.action = date_action l = ui.Button() l.title = 'Select year' l.font = ('<System-Bold>',32) l.frame = (0,0,d.width*2/3,d.height) l.background_color = 'white' d.background_color = 'white' d.add_subview(l) d.present('sheet',hide_title_bar=True) d.wait_modal()
-
It is perfect, I can now choose a year. But there is a last problem, when I use "d" as my date in the program, pythonista tells me that there is an error. Am I not allowed to use d in the program? Is there something else I need to add?
-
@pythonista21 it's because my short code also uses d as a variable, use something else like dpicker for example.
d here is an ui object, if you want the year it is str(d.date)[:4]Put after the wait_modal
sel_year = str(d.date)[:4]
And use sel_year as date variable
-
I added this line and replaced all the "d" by "sel_year" in the program but it remains the same. The error is: "can't multiply sequence by non-int of type 'float' " when I try to multiply sel_year. Pythonista still doesn't consider sel_year as a variable. I can't use it as a number and multiply or divide it. I don't understand where the problem comes from 😓
-
@pythonista21 use int instead of str if your code needs a number. As I don't know your entire code, it was not possible to know how you used this year
sel_year = int(str(d.date)[:4])
-
It works! Thanks a lot for your precious help!!
-
-
@pythonista21 , date has a day, month and year property you can access. So date.year, is an int.
-
@cvp I have one last problem that I discovered, when I wanted to chose the year 2018.
If I want to select a date I need to move my finger until I reach the date that I want. As soon as I'm removing my finger from the screen, the year that I selected is saved in the code and the solar system appears.
I can't choose 2018 because when I start the program, I'm directly on 2018 and even if I move to an other year and come back to 2018 nothing happens when I stay on 2018.
Is there a possibility to add an "OK" button that I can press when I choose the year that I wanted? -
import ui d = ui.DatePicker() d.mode = ui.DATE_PICKER_MODE_DATE l = ui.Button() l.title = '☀️' l.font = ('<System-Bold>',128) l.frame = (0,0,d.width*2/3,d.height) l.background_color = 'white' d.background_color = 'white' d.add_subview(l) ok = ui.ButtonItem() ok.title = 'ok' def ok_action(sender): d.close() ok.action = ok_action d.right_button_items = [ok] d.name = 'Select year' d.present('sheet') d.wait_modal() sel_year = d.date.year print(sel_year)