alright this is weird Python - python

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:

Related

Is there a way to add audio to Tkinter video player?

When I run this code a window pops up and I can upload a video of which I can play/stop/pause the video; however the video will not have sound. Is there any way I can solve this issue? I am new to Python. Thanks.
from tkinter import *
from tkinter.filedialog import askopenfile
from tkVideoPlayer import TkinterVideo
# customtkinter.set_appearance_mode("System") # Modes: system (default), light, dark
# customtkinter.set_default_color_theme("blue") # Themes: blue (default), dark-blue, green
# create box
window = Tk()
# title of the box, not the text in the box
window.title("Tkinter Play Videos in Video Player")
window.geometry("700x450")
window.configure(bg="white")
def open_file():
file = askopenfile(mode='r', filetypes=[
('Video Files', ["*.mp4"])])
if file is not None:
global filename
filename = file.name
global videoplayer
videoplayer = TkinterVideo(master=window, scaled=True)
videoplayer.load(r"{}".format(filename))
videoplayer.pack(expand=True, fill="both")
videoplayer.play()
def playAgain():
print(filename)
videoplayer.play()
def StopVideo():
print(filename)
videoplayer.stop()
def PauseVideo():
print(filename)
videoplayer.pause()
# center this label
lbl1 = Label(window, text="Video Player", bg="white",
fg="blue", font="none 24 bold")
lbl1.config(anchor=CENTER)
lbl1.pack()
openbtn = Button(window, text='Open', command=lambda: open_file())
openbtn.pack(side=TOP, pady=2)
playbtn = Button(window, text='Play Video', command=lambda: playAgain())
playbtn.pack(side=TOP, pady=3)
stopbtn = Button(window, text='Stop Video', command=lambda: StopVideo())
stopbtn.pack(side=TOP, padx=4)
pausebtn = Button(window, text='Pause Video', command=lambda: PauseVideo())
pausebtn.pack(side=TOP, padx=5)
window.mainloop()
The result video does not have audio.

Fetching data from uploaded MS Access file with Tkinter

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()

Copying files with progressbar?

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()

How do I get return self.func(*args) to work in Tkinter Python?

I have just started my first Python project (sorry if this is a dumb question I am just starting out) and am using the Tkinter interface. I seem to keep on receiving this prompt:
Exception in Tkinter callback
Traceback (most recent call last):
File "D:\Apps\Python\lib\tkinter_init_.py", line 1883, in call
return self.func(*args)
File "c:/Users/noahc/Documents/Programming/AppLauncher/AppLauncher.py", line 62, in
resetList = tk.Button(root, text="Reset", padx=10, pady=5, fg="white", bg="#263D42", command=lambda:[removeSave(),resetList()])
TypeError: 'Button' object is not callable
I have provided the code below:
#########################
# CREATED BY NOZZYPOZZY #
#########################
import tkinter as tk
from tkinter import filedialog, Text
import os
root = tk.Tk()
root.title("App Launcher")
root.iconbitmap('C:/Users/noahc/Documents/Programming/AppLauncher/icon.ico')
root.resizable(0,0)
apps = []
if os.path.isfile('save.txt'):
with open('save.txt','r') as f:
tempApps = f.read()
tempApps = tempApps.split(',')
apps = tempApps
apps = [x for x in tempApps if x.strip()]
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)
def resetList():
for widget in frame.winfo_children():
widget.destroy()
def removeSave():
os.remove('save.txt')
canvas = tk.Canvas(root, height=700, width=700, bg="gray")
canvas.pack()
frame = tk.Frame(root, bg="white")
frame.place(relwidth=0.8, relheight=0.8, relx=0.1, rely=0.1)
openFile = tk.Button(root, text="Open File", padx=10, pady=5, fg="white", bg="#263D42", command=addApp)
openFile.pack()
runApps = tk.Button(root, text="Run Apps", padx=10, pady=5, fg="white", bg="#263D42", command=runApps)
runApps.pack()
resetList = tk.Button(root, text="Reset", padx=10, pady=5, fg="white", bg="#263D42", command=lambda:[removeSave(),resetList()])
resetList.pack()
for app in apps:
label = tk.Label(frame, text=app)
label.pack()
root.mainloop()
with open('save.txt', 'w') as f:
for app in apps:
f.write(app + ',')
Your function resetList shares a name with your Button variable. Instead of your function, it tries to call itself.
On this line:
resetList = tk.Button(root, text="Reset", padx=10, pady=5, fg="white", bg="#263D42", command=lambda:[removeSave(),resetList()])
You're using the lambda function to execute the function removeSave and resetList. The problem is that the variable you're assigning this button too is also called resetList. We can check that when we replace the function call and instead print out what resetList is:
resetList = tk.Button(root, text="Reset", padx=10, pady=5, fg="white", bg="#263D42", command=lambda:[removeSave(),print(repr(resetList))])
Upon execution this will give us
<tkinter.Button object .!button3>
You called your function resetList, but overwrote that with the button that is also called resetList, so instead of executing the function with resetList(), your program tries to execute the button. You can fix that for example by renaming your Button variable, e.g.:
reset_list_button = tk.Button(root, text="Reset", padx=10, pady=5, fg="white", bg="#263D42", command=lambda:[removeSave(),resetList()])
reset_list_button.pack()
I suppose you mean that after you hit reset, then "Open File" and it populates your list with the previous entries? When you execute resetList, you just remove the items from the display, but keep your apps list populated, so when you call addApp, this list is loaded and displayed again with the new element at the end. You could fix that by calling apps.clear() in your resetList function, which will delete all entries in apps. – answer by #Talon

TypeError: startfile: filepath should be string, bytes or os.PathLike, not _io.TextIOWrapper

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.askopenfile(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="#263d42")
canvas.pack()
frame= tk.Frame(root, bg="white")
frame.place(relwidth=0.8, relheight=0.8,relx = 0.1, rely=0.1)
openFile = tk.Button(root, text = "Open File", padx = 10, pady=5, fg="white", bg="#263d42",command=addApp)
openFile.pack()
runApps = tk.Button(root, text = "Run Apps", padx = 10, pady=5, fg="white", bg="#263d42", command=runApps)
runApps.pack()
root.mainloop()
The function filedialog.askopenfile() returns a file object, not its name. To get the file name, use filedialog.askopenfilename().

Categories

Resources