I have a program written with Python 3.7.4 on Windows that opens web pages in a browser. It displays the default browser and gives the user the chance to change which browser they want to use to open programs with.
The default browser is detected when the program is initialised using this technique:
from winreg import HKEY_CURRENT_USER, OpenKey, QueryValueEx
import webbroswer
reg_path = r'Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice'
chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
firefox_path = 'C:/Program Files (x86)/Mozilla Firefox/firefox.exe %s'
with OpenKey(HKEY_CURRENT_USER, reg_path) as key:
print(QueryValueEx(key, 'ProgId'))
thedefaultbrowser = (QueryValueEx(key, 'ProgId'))
thedefaultbrowser = (thedefaultbrowser[0])
thedefaultbrowser = (thedefaultbrowser[0:2])
if thedefaultbrowser == "Fi":
browser_path = firefox_path
if thedefaultbrowser == "Ch":
browser_path = chrome_path
And links are opened like this, as part of an infinite loop that happens later in the program:
while True:
# [lots of GUI code that isn't relevant]
event = input()
if event == 'pleasegetmealinknao':
x=open("C:/folder/atextfilewithalinkinit.txt", "r")
the_url1 = x.read()
webbrowser.get(browser_path).open(the_url1)
x.close()
In the same loop, input can be accepted from the user to change the browser type:
elif event == "Firefox":
browser_path = firefox_path
continue
elif event == "Chrome":
browser_path = chrome_path
continue
So I can open a webpage with this just fine. I can then change the browser type just fine, and then open another webpage in the newly user-selected browser, but - only in Windows 10.
In Windows 7, Firefox co-operates beautifully, and so does Chrome but only if Chrome is set as the default browser before the program runs. However if Firefox is set as the default browser before the program is started, as soon as I switch to Chrome and then try to open anything in Chrome, the program gets very upset and stops running. No crash, no error message or traceback, it just freezes completely and needs to be hard-restarted. Sometimes I can rescue the program from its frozen state by closing the new browser window that was just opened, but this of course defeats the purpose of the program.
HOWEVER - if I scrap all this fancy business and just accept whatever the default browser is, like this:
if event == 'pleasegetmealinknao':
x=open("C:/folder/atextfilewithalinkinit.txt", "r")
the_url1 = x.read()
webbrowser.open(the_url1)
x.close()
Then the chosen link will open happily in whatever the system default browser is, whether it be Firefox, Chrome, Internet Explorer, or anything else.
So why is this happening and how can I fix? Or do I just have to bury my dream of selectable browsers?
My guess is that this might have something to do with Chrome detecting that it is not the default browser and asking the user to change the default browser setting, that would make sense. However then the question is, how do I make the Python program ignore whatever this behaviour is. A popup related to this doesn't always appear.
EDIT: further testing and sometimes the Windows 7 pattern of behaviour also happens in Windows 10... but seemingly not all the time. This makes me think even more that it's browser-related rather than strictly OS related and has a lot to do with Chrome asking for default status, but I'm still no closer to working out what to do about it.
event = input()
or similar, plus a file calledatextfilewithalinkinit.txt
that has an URL in it. – Leotaleotardhttps://www.google.com
in it or similar. – Leotaleotard