How to display Url Image Tkinter python 3 - python

How can I visualize an url of an image with tkinter ("http://images.amazon.com/images/P/0374157065.01.LZZZZZZZ.jpg").I would like to place it in the last column of the program, in standard size, I tried many guides but none of them ever worked. Thanks very mutch

Why not bind a URL to a Label like this?
from tkinter import *
import webbrowser
def callback(event):
webbrowser.open_new(r"http://www.your-image.com")
root = Tk()
link = Label(root, text="The image", fg="blue", cursor="hand2")
link.pack()
link.bind("<Button-1>", callback)
root.mainloop()

Related

pair tkinter progress bar with wget download

I have a download button when clicked it calls a download function that downloads a video from a media url using wget
example of a video url http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4
from tkinter import *
window = Tk()
name = Entry(window, width=10)
name.grid(column=0, row=0, sticky=W)
url = Entry(window, width=10)
url.grid(column=0, row=1, sticky=W)
dl_button = Button(window, text='download', command=dl) #*********
dl_button.grid(column=2, row=0, sticky=W)
status_lable = Label(window, text='Ready')
status_lable.grid(column=0, row=1, sticky=W)
bar = Progressbar(window, length=200)
bar.grid(column=1, row=1, sticky=W)
window.mainloop()
the dl function opens up 2 threads one for the download and one for the gui, the gui has a progress bar, that i want to be updated alongside the download:
import threading
from tkinter.ttk import Progressbar
import wget
def dl():
def wg():
wget.download(
url.get(), 'C:/Users/Desktop/downloads/'+name.get()+'.mp4')
def update_progress():
status_lable.config(text='Downloading...')
# also update the progress bar, based on the progress of the download from wget
dl_thread = threading.Thread(target=wg)
progress_thread = threading.Thread(target=update_progress)
dl_thread.start()
progress_thread.start()
is this doable, is it doable with somthing other than wget, or is it simply just not doable?
thx.
ok after along research alot of trial and error, i managed to make it,
here's a demo code:
import threading
from tkinter.ttk import Progressbar
import wget
from tkinter import *
def dl():
def update_progress_bar(block_num, block_size, total_size):
bar.config(value=block_num, maximum=block_size)
# when done downloading remove the progress bar
if block_num == block_size:
bar.destroy()
print("Download Complete")
def wg():
wget.download('http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
'C:/Users/hanno/Desktop/egybest python/downloads/' + 'koko'+'.mp4', bar= update_progress_bar)
dl_thread = threading.Thread(target=wg)
dl_thread.start()
window = Tk()
dl_button = Button(window, text='download', command=dl)
dl_button.grid(column=2, row=0, sticky=W)
bar = Progressbar(window, length=200)
bar.grid(column=1, row=2)
window.mainloop()
its all about the bar parameter in wget, it takes a function and calles it every block of the download, and sends it the full size of the file, the number of blocks the full size is divided into, and the current block number, then you can update the progress bar for every block inside the called function as shown in the code, good luck all.
important:
I've been told in the comments that calling tkinter methods from an outside thread may cause the the program to crash, I couldn't find any other solution for this problem online or on my own.
thank you #TheLizzard and #Rory

How do i turn off the button "glowing" on python tkinter (Linux)?

So, i've been learning a bit of tkinter and i've just found out that some things like buttons work differently from windows to linux... On windows everything works fine but on linux, when i over the button (with an image) it glows and, honestly, i can't figure out how to fix this.
from tkinter import *
import tkinter as tk
import PySimpleGUI as sg
root = tk.Tk()
root.title("Test")
root.geometry("500x500")
close_icon = PhotoImage(file='assets/Close Button.png')
close_button = tk.Button(root, image=close_icon, command=root.destroy, borderwidth=0)
close_button.pack()
if __name__ == "__main__":
root.mainloop()
What it should always look like:
What it looks like when i hover it:
Try to set option activebackground to be the same background color of the window.
color = root.cget("background")
close_button.configure(activebackground=color)

Gif image not working in tkinter

from tkinter import *
photo = PhotoImage(file="C:\Temp\test\computer.gif")
lbl = Label(root, image=photo, height="10", width="20").pack
I have absolutely no idea why this won't work it comes up with: _tkinter.TclError: couldn't recognize data in image file "C:\Temp\test\computer.gif.
Windows filenames always have to be entered as raw strings (in all of python, not just with tkinter). Also, you'll have to make a root window first. Try this:
from tkinter import *
root = Tk()
photo = PhotoImage(file=r"C:\Temp\test\computer.gif")
Label(root, image=photo, height="10", width="20").pack()
root.mainloop()

Unable to place the buttons at required positions using tkinter

I tried the below code but the buttons are placed only at the center only,it is not placed according to my required positioning
from Tkinter import *
import tkFileDialog
from PIL import ImageTk, Image
root = Tk()
def DT(event):
print "Decision tree is selected"
button3 = Button(root, text="Decision Tree",font=("Helvetica", 15))
button3.place(x=50,y=220)
button3.pack()
button3.bind('<Button-1>', DT)
root.minsize(width=1300, height=700)
#root.configure(background='lavender')
root.mainloop()
button3.pack() overrides the placement made by button3.place().
Delete the button3.pack() line.
You are placing and packing the button. Just use either place or pack.

Is there any way to delete label or button from tkinter window and then add it back?

Something like this:
from Tkinter import *
root = Tk()
but = Button(root, text = "button")
but.pack()
#When I try:
but.destroy()
but.pack()
I get an error:
TclError: bad window path name ".37111768"
The pack_forget method will hide the widget and you can pack or grid it again later.
http://effbot.org/tkinterbook/pack.htm
I have managed to get it working :) here is my work:
from Tkinter import *
def changebutton():
but.destroy()
secondbut=Button(root,text="changed")
secondbut.pack()
if __name__=='__main__':
root=Tk()
global but
but= Button(root,text="button",command=changebutton)
but.pack()
root.mainloop()

Categories

Resources