omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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

    Pythonista
    5
    25
    7862
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ccc
      ccc last edited by ccc

      def button_action(sender):
          if sender.title =="Apple":
              url = "https://www.apple.com"
          elif sender.title =="Google":
              url = "https://www.google.com"
      
      Cheng 1 Reply Last reply Reply Quote 0
      • Cheng
        Cheng @ccc last edited by

        @ccc thereโ€™s many links in a file, Function cannot pass variables.

        1 Reply Last reply Reply Quote 0
        • ccc
          ccc last edited by ccc

          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"

          cvp Cheng 2 Replies Last reply Reply Quote 0
          • cvp
            cvp @ccc last edited by

            @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')
            
            1 Reply Last reply Reply Quote 1
            • Cheng
              Cheng @ccc last edited by

              @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.

              cvp 2 Replies Last reply Reply Quote 0
              • ccc
                ccc last edited by

                Do you use GitHub? A repo with more complete code would help a lot. You definitely do not need 20 actions.

                Cheng 1 Reply Last reply Reply Quote 0
                • cvp
                  cvp @Cheng last edited by

                  @Cheng I'm sure @ccc will answer in one minute, thus...๐Ÿ˜€

                  1 Reply Last reply Reply Quote 1
                  • cvp
                    cvp @Cheng last edited by cvp

                    @Cheng when you create your buttons, set

                    button.href = href
                    

                    and you want to use it, in action, do

                    .......open(sender.href)
                    
                    
                    1 Reply Last reply Reply Quote 0
                    • Cheng
                      Cheng @ccc last edited by

                      Iโ€™ll try.
                      Thank you @ccc and thank @cvp .
                      Also thanks to Google Translate.๐Ÿ˜…

                      cvp 1 Reply Last reply Reply Quote 0
                      • cvp
                        cvp @Cheng last edited by

                        @Cheng Sorry for the French sentence, it was only a joke for @ccc

                        1 Reply Last reply Reply Quote 0
                        • ccc
                          ccc last edited by ccc

                          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)
                          
                          1 Reply Last reply Reply Quote 0
                          • Cheng
                            Cheng last edited by

                            I almost finished my code.
                            But still have a problem
                            When I tapped button0 TAP ,button0 does not disappear as sender.title = โ€˜ โ€˜, and the new button has displayed.
                            And how to use remove_subview(button0)?

                            import requests
                            import re
                            import json
                            import ui, appex
                            import webbrowser
                            
                            headers={
                            	'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
                            }
                            data = {
                            	'cate':'realtimehot'
                            }
                            
                            
                            def get_page(url):
                            	response = requests.get(url)
                            	html = response.text
                            	#print(html)
                            	return html
                            
                            
                            def parse_html(html):
                            	#top = re.findall(r'class="icon-top"></i></td>.*?<td class="td-02">.*?<a href="(.*?)" target="_blank">(.*?)</a>.*?</td>', html, re.S)
                            	data = re.findall(r'ranktop">(.*?)</td>.*?<a href="(.*?)" target="_blank">(.*?)</a>.*?<span>(.*?)</span>', html, re.S)
                            	#print(top)
                            	#print(data)
                            	return data
                            
                            
                            def write_data(data):
                            	with open(r'./wb.json', 'w', encoding='gbk') as f:
                            		for i in data:
                            			f.writelines(json.dumps(i) + '\n')
                            	print('done')
                            	
                            
                            def get_json(items, host):
                            	for item in items:
                            		yield {
                            			'rank': item[0],
                            			'href': host + item[1],
                            			'title': item[2],
                            			'num': item[3],
                            		}
                            
                            class control(ui.View):
                            	def __init__(self):
                            		self.frame=(0,0,400,600)
                            		appex.set_widget_view(self)
                            		
                            		
                            	def button_tapped1(self, sender):
                            		webbrowser.open(sender.href)
                            
                            		
                            	def button_tapped(self, sender):
                            		sender.title = ''
                            		self.list()
                            	
                            
                            	def layout(self):
                            		button0 = ui.Button(font=('<System>', 34), frame=(100,25,200,40), tint_color='red')
                            		button0.title = 'TAP'
                            		button0.action = self.button_tapped
                            		self.add_subview(button0)
                            		return button0
                            	
                            
                            	def list(self):
                            		host = 'https://s.weibo.com'
                            		url = 'http://s.weibo.com/top/summary?'
                            		html = get_page(url)
                            		data = parse_html(html)
                            		wb = get_json(data, host)
                            		write_data(wb)
                            		with open('./wb.json', 'r') as f:
                            			wb = f.readlines()
                            		for i in range(0,23):
                            			wbi = wb[i]
                            			title = json.loads(wbi)['title']
                            			href = json.loads(wbi)['href']
                            			#title = json.loads(wbi)
                            			button = ui.Button(title=title, font=('<System>', 18))
                            			button.href = 'Alook://' + href
                            			button.action = self.button_tapped1
                            			button.frame = (0,i*25,400,20)
                            			self.add_subview(button)
                            
                            	
                            def main():
                            	control()
                            
                            
                            if __name__ == '__main__':
                            	main()
                            
                            
                            1 Reply Last reply Reply Quote 0
                            • ccc
                              ccc last edited by

                              # This button will self destruct.
                              def button_tapped1(self, sender):
                                      webbrowser.open(sender.href)
                                      self.remove_subview(sender)
                              
                              1 Reply Last reply Reply Quote 0
                              • cvp
                                cvp last edited by

                                The real problem comes from the fact that layout is called two times, thus there are two buttons with TAP title...
                                Create your button0 in init and remove the layout def.
                                Or rename your layout into layoutx and call it in init:

                                    def __init__(self):
                                        self.frame=(0,0,400,600)
                                        appex.set_widget_view(self)
                                        self.layoutx()
                                .
                                .
                                .
                                    def layoutx(self):
                                
                                Cheng 1 Reply Last reply Reply Quote 0
                                • Cheng
                                  Cheng @cvp last edited by

                                  @cvp You are right . Function name can't be written casually.

                                  1 Reply Last reply Reply Quote 0
                                  • First post
                                    Last post
                                  Powered by NodeBB Forums | Contributors