Programatically opening URLs in web browser in Python - python

I have a tkinter script. I was wondering is there anyway to have is so when you hit a button it takes you to a Web Site
from tkinter import *
app = Tk()
app.geometry("250x400")
app.title("Links")
def Link():
?
button1 = Button(app, text = "To a web site.", command = Link)
button1.pack()
app.mainloop()

There's a module for that.
import webbrowser
webbrowser.open("http://xkcd.com/353/")

Use webbrowser module to open the URL. The documentation: https://docs.python.org/3/library/webbrowser.html
# You can use open
from webbrowser import open
def link():
open('https://www.youtube.com/')
# You can use open_new_tab
from webbrowser import open_new_tab
def link():
open_new_tab('https://www.youtube.com/')

Related

Python TkInter create a button to open a specific directory

I want to create a button that opens the special directory like "C:\Users\", But as I searched, I did not find any code except the following code:
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
gui = Tk()
gui.geometry("100x100")
def getFolderPath():
filedialog.askdirectory()
btnFind = ttk.Button(gui, text="Open Folder",command=getFolderPath)
btnFind.grid(row=0,column=2)
gui.mainloop()
What is the correct way to solve this problem?
Use os.startfile("C:\\Users") to use the system file explorer to open the directory.

Python opening Default Browser

So I have been coding a search bar type thing in Python because I was bored and all. And when searching something with it, it opens in Internet Explorer instead of my default browser (Chrome).
from tkinter import *
import webbrowser
win = Tk()
win.title("Search Bar")
def search():
url = entry.get()
webbrowser.open(url)
label1 = Label(win,text="Enter URL Here : "
,font=("arial",10,"bold"))
label1.grid(row=0,column=0)
entry = Entry(win,width=30)
entry.grid(row=0,column=1)
button = Button(win,text="Search",command=search)
button.grid(row=1,column=0,columnspan=2,pady=10)
win.mainloop()
try like this
webbrowser.get('chrome').open('https://stackoverflow.com/')
OR
webbrowser.get("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s").open("http://google.com")

Python Pyinstaller exe opens multiple instance of tkinter window, Python Firebase [Video]

I have a scraping script of where im using tkinter for ui. When i build the exe(with pyinstaller) and open it it working well, But When i close it, it opens multiple instance of tkinter Window. I cant paste the full code. So i pasted all the tkinter code i am using.
Here is the Full code Github Gist here
import requests
from lxml import html
from tkinter import *
import tkinter as ttk
import re
import datetime
import os
from firebase import firebase
import hashlib
#import App as App
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
#Region Tk
root = Tk()
root['height'] = 400
root['width'] = 600
global firebase
firebase = firebase.FirebaseApplication('#######URL####',None)
f1 = Frame(root)
f1["height"] = root["height"]
f1["width"] = root["width"]
root.title("JD Scraper - Gear Up Studio ")
Label(f1,text = "Input Url : Example : https://www.justdial.com/Ahmedabad/Gyms ").grid(row=0,column = 0,)
def getBool(event):
print(boolvar.get())
#Check Button
global boolvar
boolvar = BooleanVar()
boolvar.set(False)
boolvar.trace('w', lambda *_: print("The value was changed"))
cb = Checkbutton(f1, text = "Tele Phone number", variable = boolvar)
cb.bind("<Button-1>", getBool)
cb.grid(row=1, column=1)
global key_filled
key_filled = Entry(f1,width=50)
key_filled.grid(row=2,column=0)
key_filled.focus_set()
global activate_button
activate_button = Button(f1 , text="Active Now")
activate_button.bind("<Button-1>",activate_key)
activate_button.grid(row=2, column=1)
result = Label(f1, width=50)
result.grid(row=1,column=2)
global submit_button
submit_button = Button(f1 , text="Scrape Now")
submit_button.bind("<Button-1>",button_clicked)
submit_button.grid(row=1, column=0)
submit_button.config(state=NORMAL)
key_validation()
f1.pack()
root.mainloop()
Demo Video here
i was facing the exact the same problem with firebase and pyqt5. After many tries i examine the firebase library. In init there is close_process_pool() function which is called when program exits and close all the multiprocessing pools. In tkinter and pyqt case we dont need this function as all the process is the childs of main GUI thread so simple removing that function resolves the problem.
Change the init file to this will resolve the issue.
import atexit
#from .async import process_pool
from firebase import *
'''
#atexit.register
def close_process_pool():
"""
Clean up function that closes and terminates the process pool
defined in the ``async`` file.
"""
process_pool.close()
process_pool.join()
process_pool.terminate()
'''
I can see cmd window pops when run your exe to remove that use this command -w and also to compile as onefile into dist folder use -F to compile it as a single file, do this to resolve the issue
pyinstaller -w -F replace this with your script name.py
It will compile the file as one file for you and the use will be res

How to open a exe file using python Tkinter

I am trying to open a exe file from C drive by clicking a button from my GUI and up till now i am unable to select the specific file. Can i know if there is any function to directly open the file in Tkinter. Currently,tkFileDialog.askdirectory only direct me to the FILES.
import Tkinter
import tkMessageBox
import tkFileDialog
import os
import subprocess
top = Tkinter.Tk()
def run():
File = tkFileDialog.askdirectory()
os.system(File)
b = Tkinter.Button(top, text = 'DAQoutput', command= run)
b.pack()
top.mainloop()
Directly using Tkinter? No there is no command for it but you don't need one. Tkinter is a GUI toolkit and that's all it does.
Instead use os.system() to launch your exe.
Your script should look something like this:
import Tkinter
import tkMessageBox
import tkFileDialog
import os
top = Tkinter.Tk()
def run(self):
File = tkFileDialog.askdirectory()
os.system(File)
b = Tkinter.Button(top, text = 'DAQoutput', command= self.run)
b.pack()
top.mainloop()

Tkinter program runs button command on startup?

So I have a program that is basically supposed to have a button that opens a file dialog in the (username) folder. But when I run the program it opens without even pushing the button. What's more, the button doesn't even show up. So in addition to that problem I have to find a way to turn the selected directory into a string.
import tkinter
import tkinter.filedialog
import getpass
gui = tkinter.Tk()
user = getpass.getuser()
tkinter.Button(gui, command=tkinter.filedialog.askopenfilename(initialdir='C:/Users/%s' % user)).pack()
gui.mainloop()
Regarding your first issue, you need to put the call to tkinter.filedialog.askopenfilename in a function so that it isn't run on startup. I actually just answered a question about this this morning, so you can look here for the answer.
Regarding your second issue, the button isn't showing up because you never placed it on the window. You can use the grid method for this:
button = tkinter.Button(gui, command=lambda: tkinter.filedialog.askopenfilename(initialdir='C:/Users/%s' % user))
button.grid()
All in all, your code should be like this:
import tkinter
import tkinter.filedialog
import getpass
gui = tkinter.Tk()
user = getpass.getuser()
button = tkinter.Button(gui, command=lambda: tkinter.filedialog.askopenfilename(initialdir='C:/Users/%s' % user))
button.grid()
gui.mainloop()
You forgot to use a geometry manager on the button:
button = tkinter.Button(window, command=test)
button.pack()
If you don't do it, the button won't be drawn. You might find this link useful: http://effbot.org/tkinterbook/pack.htm.
Note that to pass the command to handler you have to write only the name of the function (Like it's descibed in the other answer).
This is an old question, but I just wanted to add an alternate method of preventing Tkinter from running methods at start-up. You can use Python's functools.partial (doc):
import tkinter
import tkinter.filedialog
import getpass
import functools
gui = tkinter.Tk()
user = getpass.getuser()
button = tkinter.Button(
gui,
command=functools.partial(
tkinter.filedialog.askopenfilename,
initialdir='C:/Users/%s' % user)
)
button.grid()
gui.mainloop()

Categories

Resources