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.
    • Cheng
      Cheng last edited by

      It use in widget.
      1.get date in a json file
      2.get date from datetime
      3.If the two dates are the same, read the json file,else update the json file and read it.
      4.put the result in widget.

      I am a newbie, please advise.

      And I want to ask how to display multiple buttons in the widget and can turn pages?

      # -*- coding: UTF-8 -*-
      
      import requests
      import re
      import json
      import datetime
      import appex, ui
      
      headers = {
      'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
      'Accept-Language': 'zh-CN,zh;q=0.9'
      }
      
      
      def get_page(url):
          response = requests.get(url, headers=headers)
          data = json.loads(response.text)
          return data
      
      
      def write_file(data):
          with open(r'./mryj.json', 'w+') as f:
              f.write(json.dumps(data))
              print('文件已更新')
      
      
      def get_filedate():
          try:
              with open(r'./mryj.json', 'r') as f:
                  d = json.loads(f.read())
                  filedate = d['dateline']
                  print('文件日期 ', filedate)
          except:
              with open(r'./mryj.json', 'w+') as f:
                  d = f.write(json.dumps({'dateline':'None'}))
                  filedate = 'None'
          return filedate
      
      
      def get_datetime():
          timenow = datetime.datetime.now().strftime('%Y-%m-%d')
          print('实际日期 ', timenow)
          return timenow
      
      
      
      def layout():
      	with open(r'./mryj.json', 'r') as f:
      		data = json.loads(f.read())
      	v = ui.View()
      	text = ui.TextView(font=('Didot', 15))
      	text.text = '   ' + data['note'] + '\n' + '  ' + data['content']
      	text.editable = False
      	text.selectable = False
      	v.add_subview(text)
      	appex.set_widget_view(text)
      	
      url = 'http://open.iciba.com/dsapi/'
      
      filedate = get_filedate()
      timenow = get_datetime()
      if filedate == timenow:
          print('时间相同,读取文件')
          layout()
      else:
          print('时间不同,更新文件')
          data = get_page(url)
          write_file(data)
          layout()
      
      
      cvp 1 Reply Last reply Reply Quote 0
      • ccc
        ccc last edited by

        def get_page(url):
            return requests.get(url, headers=headers).json()
        
        1 Reply Last reply Reply Quote 0
        • cvp
          cvp @Cheng last edited by cvp

          @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

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

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

              
              button1.frame = (x, y,width,height)
              button2.frame = (x, y,width,height)
              
              v.add_subview(button1)
              v.add_subview(button2)
              
              Cheng 1 Reply Last reply Reply Quote 0
              • Cheng
                Cheng @brumm last edited by

                @brumm I succeeded
                I didn't set the position when I tried it, the second button blocked the first button.
                Thank U.

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

                  @Cheng you are right, and you made a little error with add_subview(button) instead (button1) and (button2)

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

                    @Cheng, for paging, how long are your lines?

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

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

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

                        And another problem is Button.action
                        The function must take a single sender parameter.
                        I want different buttons to correspond to different links.

                        1 Reply Last reply Reply Quote 0
                        • 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
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors