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.
It's my first program for Pythonista
-
def get_page(url): return requests.get(url, headers=headers).json()
-
@Cheng you have to use v instead of text as widget_view
appex.set_widget_view(v)
and add buttons as subviews of v, like you did with textview
-
@cvp Thank u.
Can you give me an example with the following code?
In a widget display both button1 and button2.v = ui.View() button1 = ui.Button(title='button1') button2 = ui.Button(title='button2') v.add_subview(button) appex.set_widget_view(v)
-
button1.frame = (x, y,width,height) button2.frame = (x, y,width,height) v.add_subview(button1) v.add_subview(button2)
-
@brumm I succeeded
I didn't set the position when I tried it, the second button blocked the first button.
Thank U. -
@Cheng you are right, and you made a little error with add_subview(button) instead (button1) and (button2)
-
@Cheng, for paging, how long are your lines?
-
@mikael I plan to do less than 50 lines.
Each button is actually a hyperlink that allows browse the web in the widget.That's my idea. -
And another problem is Button.action
The function must take a single sender parameter.
I want different buttons to correspond to different links. -
def button_action(sender): if sender.title =="Apple": url = "https://www.apple.com" elif sender.title =="Google": url = "https://www.google.com"
-
@ccc there’s many links in a file, Function cannot pass variables.
-
The button (sender) has attributes... Here I used sender.title but you could also add a .url attribute to your buttons...
button1.url = "https://www.apple.com"
-
@ccc "les grands esprits se rencontrent" 😂, I just wrote
import ui v = ui.View() v.frame = (0,0,200,200) def b_action(sender): print(sender.url) b1 = ui.Button(title='Apple', frame = (10,10,80,32), url='https://www.apple.com', action=b_action) v.add_subview(b1) b2 = ui.Button(title='Google', frame = (100,10,80,32), url='https://www.google.com', action=b_action) v.add_subview(b2) v.present('sheet')
-
@ccc my button url is a variable.
def button_tapped(sender): self.webbrowser.open(href) #??? v=ui.View(frame=(0,0,300,400)) with open('./wb.json', 'r') as f: wb = f.readlines() for i in range(0,20): wbi = wb[i] title = json.loads(wbi)['title'] href = json.loads(wbi)['href'] #href is the button url button = ui.Button(title=title) button.action = button_tapped button.frame = (0,i*25,450,20) v.add_subview(button)
I think I need 20 button.tapped functions.
-
Do you use GitHub? A repo with more complete code would help a lot. You definitely do not need 20 actions.
-
-
@Cheng when you create your buttons, set
button.href = href
and you want to use it, in action, do
.......open(sender.href)
-
-
-
You can write your data to a .json file and then read it back into a dict...
#!/usr/bin/env python3 import json data = { "Apple": "https://www.apple.com", "Google": "https://www.google.com", "IBM": "https://www.ibm.com", } with open("buttons.json", "w") as out_file: json.dump(data, out_file) del data # remove the in-memory instance with open("buttons.json") as in_file: data = json.load(in_file) for i, (title, url) in enumerate(data.items()): print(i, title, url)