I'm making a simple program that uses tkinter and shutil to copy files, nothing fancy. The problem is that there's no indication of when the process is done, and if I'm copying large folders, the program freezes until it's done copying. I want a progressbar, but don't know how to implement one in tkinter.
from tkinter import *
from tkinter import filedialog
import shutil
def browseFiles():
filename = filedialog.askdirectory(initialdir="/", title="Select a directory")
label_file_explorer.configure(text="Directory Opened: " + filename, bg="grey")
shutil.copytree(filename, r"J:\PCmover\ziptest\test")
window = Tk()
window.title('Beringia')
window.geometry("700x500")
window.config(background="white")
label_file_explorer = Label(window, width=100, height=4, bg="white", fg="blue")
button_explore = Button(window, text="Choose directory", command=browseFiles)
button_exit = Button(window, text="Quit", command=exit)
softwarename=Label(window, text="Welcome to Beringia!", width=100, height=4, fg="blue")
ver=Label(window, text="Ver 1.0", bg="grey", fg="blue")
label_file_explorer.place(x=0, y=200)
button_explore.place(x=305, y=80)
button_exit.place(x=335, y=120)
softwarename.place(x=0, y=0)
ver.place(x=659, y=479)
window.mainloop()
Related
I'm having a hard time fetching my data from the uploaded MS Access file. I just want to select and upload .mdb file and then display the data inside the frame according to my query line.
This is my code and I got stuck.
import tkinter as tk
from tkinter import filedialog, Text
import os
root = tk.Tk()
def addFile():
filename = filedialog.askopenfilename(initialdir="/", title="Select File", filetypes=(("Microsoft Access", "*.mdb *.accdb"), ("all files", "*.*")))
canvas = tk.Canvas(root, height=500, width=500, bg="#263d42")
canvas.pack()
openFile = tk.Button(root, text="Open File", padx=10, pady=5, fg="#263d42", bg="white", command= addFile)
openFile.pack()
frame = tk.Frame(root, bg="white")
frame.place(relwidth=0.8, relheight=0.8, relx=0.1, rely=0.1)
root.mainloop()
so ok i was just using TKInter when i got this error NameError
name 'runApps' is not defined
import tkinter as tk
from tkinter import filedialog, Text
import os
root = tk.Tk()
apps = []
def addApp():
for widget in frame.winfo_children():
widget.destroy()
filename = filedialog.askopenfilename(initialdir="/", title="Select File",
filetypes=(("executables","*.exe"), ("all files", "*.*")))
apps.append(filename)
print(filename)
for app in apps:
label = tk.Label(frame, text=app, bg="gray")
label.pack()
def runApps():
for app in apps:
os.startfile(app)
canvas = tk.Canvas(root, height=700, width=700, bg="#163542")
canvas.pack()
frame = tk.Frame(root, bg="green")
frame.place(relwidth=0.4, relheight=0.4, relx=0.3, rely=0.25)
openFile = tk.Button(root, text="Open File", padx=10,
pady=5, fg="white", bg="#163542" ,command=addApp)
openFile.pack()
runApps = tk.Button(root, text="Run Apps", padx=10,
pady=5 , fg="white", bg="#163542" , command = runApps)
runApps.pack()
root.mainloop()
ok if anyone knows how to fix this make a comment or answer
this section for this post is only for ShanyeLoyd
here is the post you wanted ShanyeLoyd
If you fix the indents in your code then it works.
As per the official documentation:
Leading whitespace (spaces and tabs) at the beginning of a logical
line is used to compute the indentation level of the line, which in
turn is used to determine the grouping of statements.
Correct formatted code:
import tkinter as tk
from tkinter import filedialog
import os
root = tk.Tk()
apps = []
def addApp():
for widget in frame.winfo_children():
widget.destroy()
filename = filedialog.askopenfilename(
initialdir="/",
title="Select File",
filetypes=(("executables", "*.exe"), ("all files", "*.*")),
)
apps.append(filename)
print(filename)
for app in apps:
label = tk.Label(frame, text=app, bg="gray")
label.pack()
def runApps():
for app in apps:
os.startfile(app)
canvas = tk.Canvas(root, height=700, width=700, bg="#163542")
canvas.pack()
frame = tk.Frame(root, bg="green")
frame.place(relwidth=0.4, relheight=0.4, relx=0.3, rely=0.25)
openFile = tk.Button(
root, text="Open File", padx=10, pady=5, fg="white", bg="#163542", command=addApp
)
openFile.pack()
runApps = tk.Button(
root, text="Run Apps", padx=10, pady=5, fg="white", bg="#163542", command=runApps
)
runApps.pack()
root.mainloop()
GUI:
I'm trying to give myself Excercises using imports and keeping programs and modules separate from each other. In this particular excercise I've given myself, I have a basic feet-to-inches and inches-to-feet converter.
Everything works except for one problem. Where the answer is displayed on the GUI, a new answer covers the old answer, rather than replace.
import tkinter as tk
from tkinter import E, W
import Converter
window = tk.Tk()
window.title("Converter")
answer_label = tk.Label(window, text="No Answer")
answer_label.grid(row=5)
def feet_to_inches_function(old_label):
old_label.grid_forget()
answer_label = tk.Label(window, text=Converter.feet_to_inches(int(entry_bar.get())))
answer_label.grid(row=5)
def inches_to_feet_function(old_label):
old_label.grid_forget()
answer = tk.Label(window, text=Converter.inches_to_feet(int(entry_bar.get())))
answer.grid(row=5)
title_label = tk.Label(window, text="Convert")
entry_bar = tk.Entry(window, font=('HELVETICA', 10))
fti_button = tk.Button(window, text="Feet to Inches", command=lambda: feet_to_inches_function(answer_label))
itf_button = tk.Button(window, text="Inches to Feet", command=lambda: inches_to_feet_function(answer_label))
quit_button = tk.Button(window, text="Quit", command=window.destroy)
title_label.grid(row=0, column=0, sticky=W)
entry_bar.grid(row=1, columnspan=2, sticky=(E, W))
fti_button.grid(row=3, column=0)
itf_button.grid(row=3, column=1)
quit_button.grid(row=4, columnspan=2, sticky=(E, W))
window.mainloop()
Also as a separate question,I'm having a hard time understanding mainloop.I know that it makes the program go in an infinate loop, but from where? I don't see any logical evidence that shows that my code is on an endless loop.
Thanks so much
Here's a solution for you:
import tkinter as tk
from tkinter import E, W
window = tk.Tk()
window.title("Converter")
answer_label = tk.Label(window, text="No Answer")
answer_label.grid(row=5)
def feet_to_inches_function():
answer_label.configure(text='As you want (INCHES)')
def inches_to_feet_function():
answer_label.configure(text='As you want (FEET)')
title_label = tk.Label(window, text="Convert")
entry_bar = tk.Entry(window, font=('HELVETICA', 10))
fti_button = tk.Button(window, text="Feet to Inches", command=feet_to_inches_function)
itf_button = tk.Button(window, text="Inches to Feet", command=inches_to_feet_function)
quit_button = tk.Button(window, text="Quit", command=window.destroy)
title_label.grid(row=0, column=0, sticky=W)
entry_bar.grid(row=1, columnspan=2, sticky=(E, W))
fti_button.grid(row=3, column=0)
itf_button.grid(row=3, column=1)
quit_button.grid(row=4, columnspan=2, sticky=(E, W))
window.mainloop()
All you need is to configure the text of the Label in both functions.
The program works fine on other computers that run window's or ubuntu but the text won't show on the buttons. Here's a snippet of a longer code:
from Tkinter import *
from math import atan
from math import log
import math
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
# --- function ---
def create_first_frame():
global root
global frame
# frame.destroy()
frame = Frame(bg="blue")
frame.pack()
label1 = Label(frame, text="hello!", fg="white", bg="blue", font="normal 30")
label1.pack()
button1 = Button(frame, text="Enter", fg="white", bg="blue", font="normal 20")
button1.pack()
button2 = Button(frame, text="Exit", font="normal", fg="white", bg="red", command=root.destroy)
button2.pack(side=LEFT)
root = Tk()
create_first_frame()
root.mainloop()
We were expecting words like "start" and "exit" to show.
We just want the colors of the buttons to show along with the text
I am not sure what the question is. I took out unnecessary imports, and ran
from tkinter import *
def create_first_frame():
global root
global frame
# frame.destroy()
frame = Frame(bg="blue")
frame.pack()
label1 = Label(frame, text="hello!", fg="white", bg="blue", font="normal 30")
label1.pack()
button1 = Button(frame, text="Enter", fg="white", bg="blue", font="normal 20")
button1.pack()
button2 = Button(frame, text="Exit", font="normal", fg="white", bg="red", command=root.destroy)
button2.pack(side=LEFT)
root = Tk()
create_first_frame()
root.mainloop()
Which creates for me:
This is colored as you have specified...
I am trying to learn Tkinter and python. How can I open multiple images and save it on multiple canvas in Python 3? I also want the image to fit just perfect on to the size of the canvas.
Could someone show me to do this using simple code.
import tkinter
from tkinter import filedialog
import os
#from PIL import ImageTk, Image
from tkinter import *
import PIL.Image
from tkinter.filedialog import askopenfilename
import py_compile
mainWindow =tkinter.Tk()
mainWindow.title("Image")
mainWindow.geometry('640x480+800+200')
mainWindow.columnconfigure(0, weight=3)
mainWindow.columnconfigure(1, weight=1)
mainWindow.columnconfigure(2, weight=1)
mainWindow.rowconfigure(0, weight=3)
mainWindow.rowconfigure(1, weight=3)
mainWindow.rowconfigure(2, weight=5)
mainWindow.rowconfigure(3, weight=3)
leftFrame = tkinter.LabelFrame(mainWindow, text='PICTURE')
leftFrame.grid()
canvas = tkinter.Canvas(leftFrame, relief='sunken', borderwidth=5, bg= 'white', width=100, height=100)
canvas.grid(row=1, column=0)
canvas2 = tkinter.Canvas(leftFrame, relief='sunken', borderwidth=5, bg= 'white', width=100, height=100)
canvas2.grid(row=2, column=0)
canvas3 = tkinter.Canvas(leftFrame, relief='sunken', borderwidth=5, bg= 'white', width=100, height=100)
canvas3.grid(row=1, column=1)
canvas4 = tkinter.Canvas(leftFrame, relief='sunken', borderwidth=5, bg= 'white', width=100, height=100)
canvas4.grid(row=2, column=1)
def clicked():
print('hello')
open_img()
def open_img():
global photo
filename = filedialog.askopenfilename(initialdir = "E:/Images", filetypes = ())
photo = tkinter.PhotoImage(file=filename)
photo = photo.subsample(3,3)
canvas.create_image(0,0, anchor=CENTER, image=photo)
rightFrame = tkinter.LabelFrame(mainWindow, text='MENU')
rightFrame.grid()
button1 = tkinter.Button(rightFrame , text="Open", command=clicked)
button1.grid(row=1, column=2)
mainWindow.mainloop()
You would use one class with each image creating an instance of that class, but since you have not used a class and so are probably not familiar with the class structure, the following code combines everything into one class. Since I don't have the images, this opens a new Toplevel with a related "close" button instead of an image, each time the "open" button is clicked.
try:
import Tkinter as tk ## Python 2.x
except ImportError:
import tkinter as tk ## Python 3.x
from functools import partial
class OpenToplevels():
""" open and close additional Toplevels with a button
"""
def __init__(self):
self.root = tk.Tk()
self.button_ctr=0
but=tk.Button(self.root, text="Open a Toplevel",
command=self.open_another)
but.grid(row=0, column=0)
tk.Button(self.root, text="Exit Tkinter", bg="red",
command=self.root.quit).grid(row=1, column=0, sticky="we")
self.root.mainloop()
def close_it(self, id):
id.destroy()
def open_another(self):
self.button_ctr += 1
id = tk.Toplevel(self.root)
id.title("Toplevel #%d" % (self.button_ctr))
tk.Button(id, text="Close Toplevel #%d" % (self.button_ctr),
command=partial(self.close_it, id),
bg="orange", width=20).grid(row=1, column=0)
Ot=OpenToplevels()