Using Webview for textfield
Hi everybody, I was thinking of creating a textfield that I can enter a URL and it will open that URL, if I understand i correct I need to use the "Webview" but how do I connect the Webview to the textfield. I read something about delegates, is that the way to go and how do I do it? The goal is to eventually create a simple browser (If I ever get so far) but for now I would likte to undestand Webview and how to use it in a textfield
The amount of code can be reduced quite a bit when you use a
pyuifile to configure the UI, but it's hopefully a bit easier to see what's going on when everything is set up in code.
@omz Thanks! I think this will help alot
@omz Just wanna say thanks for the help, But I have one question:
The webview is it possible for it to be an other color then white, or somehow invisible until the it is called for?
The reason for this is that I would like my "browser" to have a purpleish background but when adding the Webview it will cover most of the background. Tried using web_view.bg_color = "Purple" but it did not change (maybe it's possible to change it)
ui.WebViewonly affects the area behind the content (you see it with the "rubberband" effect when scrolling). You could just load some purple HTML though, to get the same effect:
web_view.background_color = 'purple' web_view.load_html('<body style="background-color: purple;"/>')
@omz ah thanks, didn't think of that
If I want a textfield as a seachfield for google, is there a command for web serach like there is for load_url
web_view = sender.superview['webview']
"google.com" = sender.text <-- This is where Im stuck
web_view.search_string/textl(url) <-- No such command
webview.load_url('https://www.google.com/search?q=' + sender.text)
ThX man, really appricate it!
If your search term contains spaces, or other characters that aren't allowed in a URL, you'll need to quote them, e.g.
import urllib.parse search_term = urllib.parse.quote(sender.text, '') webview.load_url('https://www.google.com/search?q=' + search_term)
Thank very much got it to work now. One step closer to my own webbrowser 😀
Hello again is there anybody that knows why I get an attribute error when pressing the "opt" button for actvating navigationview?
The whole error message reads" Attribute Error: 'NoneType' object has no attribute 'push_view'
Here is the code:
import ui import urllib.parse def textfield_url(test): web_view = test.superview['webview'] # superview = in front of other url = test.text if not (url.startswith('http://') or url.startswith('https://')): # needed for urls starting with http or https url = 'http://' + url web_view.load_url(url) def searchfield_url(test2): web_view = test2.superview['webview'] url = test2.text search_term = urllib.parse.quote(test2.text, '') web_view.load_url('https://www.google.com/search?q=' + search_term) def homeButton (sender): # Working! web_view.load_url('http://www.google.se') #url = 'www.google.se' def Back(sender2): # Working! web_view.go_back() #url = www.google.se def buttonTapped(sender3): n_view = ui.View() n_view.name = 'Option' n_view.background_color = 'white' #n_view.frame=(0, 0, 400, 400) sender3.navigation_view.push_view(n_view) <-- This is where I get the "Attribute Error" nav_view.present() main_view = ui.View( frame=(0, 0, 400, 400), bg_color='white', name='Grizzly Browser' ) text_field = ui.TextField( frame=(129, 0, 50, 40), flex='W', keyboard_type=ui.KEYBOARD_URL, autocorrection_type=False, autocapitalization_type=ui.AUTOCAPITALIZE_NONE, action=textfield_url, placeholder='URL:' #text_field ) seach_field = ui.TextField( frame=(520, 0, 0, 40), flex='w', keyboard_type=ui.KEYBOARD_WEB_SEARCH, autocorrection_type=False, autocapitalization_type=ui.AUTOCAPITALIZE_NONE, action=searchfield_url, placeholder = 'Search:' ) B1 = ui.Button(title = 'Home' ,frame = (4,5,20,20)) B1.action = homeButton B1.tint_color = 'black' B2 = ui.Button(title = 'Back' ,frame = (55,5,20,20)) B2.action = Back B2.tint_color = 'black' b3 = ui.Button(title = 'opt') b3.action = buttonTapped b3.frame = (80,0,50,50) main_view.add_subview(b3) main_view.add_subview(text_field) web_view = ui.WebView(name='webview', frame=(0, 40, 400, 360), flex='WH') nav_view = ui.NavigationView(main_view) nav_view.frame=(0, 0, 400, 400) #web_view.load_html('<body style="background-color: indigo;"/>') main_view.add_subview(web_view) main_view.add_subview(B1) main_view.add_subview(B2) main_view.add_subview(seach_field) main_view.present() web_view.load_url('http://www.feber.se') #StartPage
If I put that code here instead it will crash
Really greatfull for any help
This message is telling you that
sender3.navigation_view == None. Sender is the button, not the view that contains the button. You might try
edityour post above to see how putting three backticks before and after your code will properly format it with syntax highlighting.
@ccc Thank you for your response, gonna try it later at home.
Ok Good to know for next time