Can't click button on tkinter - python

I am new to tkinter and I am having trouble with buttons on the python GUI. I create the buttons and the function behind it but I am unable to click the button. Can someone tell me what I am doing wrong? The UI comes up and I can see the button I just can't click it.
import tkinter as tk
from tkinter import filedialog, Text
import os
#main
root = tk.Tk()
def addApp():
filename = filedialog.askopenfilename(initialir= "/", title= "Select File",
filetypes = (("executables", "*.exe"),
("all files", "*.*")))
canvas = tk.Canvas(root, height=700, width=700, bg="#33F9FF")
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="black",
bg="#33F9FF", command="addApp")
openFile.pack()
runApps = tk.Button(root, text = "Run Apps", padx = 10, pady = 5, fg="black",
bg="#33F9FF")
runApps.pack()
root.mainloop()

Very, small mistake. You added command argument as a string and not a function
openFile = tk.Button(root, text = "Open File", padx = 10, pady = 5, fg="black",
bg="#33F9FF", command=addApp)
EDIT:
there is a small typo at initialdir argument in openfiledialogbox
filename = filedialog.askopenfilename(initialdir= "/", title= "Select File",
filetypes = (("executables", "*.exe"),
("all files", "*.*")))

There's also one more mistake:
def addApp():
filename = filedialog.askopenfilename(initialdir= '/', title= "Select File",
filetypes = (("executables", "*.exe"),
("all files", "*.*")))

Related

File-path not displaying on tkinter entry box

I've looked into multiple threads and I can't seem to find out why the stringVar is not displaying on the entry box, once the file has been selected. I can see the print(filePath.get()) has the selected file but entry box stays blank.
Can someone please help?
#import libraries
import os
import paramiko
import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from tkinter.filedialog import askopenfilename
import sys
def getFilePath():
file_selected = filedialog.askopenfilename(title="Select File")
filePath.set(file_selected)
print(filePath.get())
#-------------------------------GUI------------------------------
gui = tk.Tk()
gui.title("NetWizard 1.0")
tabControl = ttk.Notebook(gui, width = 1000, height = 700)
gui.resizable(False, False)
tabControl.pack( expand = 1, fill ="both")
tab1 = ttk.Frame(tabControl)
tab2 = ttk.Frame(tabControl)
tabControl.add(tab1, text ='RUGGEDCOM', )
tabControl.add(tab2, text ='WESTERMO')
#--------------------------RUGGEDCOM WIDGET----------------------
filePath = StringVar()
entry1 = Entry(gui, textvariable=filePath, width=1000)
entry1 = tk.Entry(tab1, width = 60)
label1 = Label(tab1, text="IP Address List Input:", font=('Arial', '11', 'bold'))
button1 = Button(tab1, text='Browse', command=getFilePath, font=("Arial", 10), width = 10, bg='#add8e6')
label1.grid(row=0, column=0, padx = 15, pady=10, sticky = W)
entry1.grid(row=1, column=0, padx=15, pady=5)
button1.grid(row=1, column=1, padx=0, pady=0, sticky = W)
gui.mainloop()
It is because you did not set your entry text value in your function. Supposedly after you get your file path, you should assign the file path text value into the entry.
You can do so by inserting this code into your codes.
entry1.insert(0, "Test Value")
Your codes:
def getFilePath():
file_selected = filedialog.askopenfilename(title="Select File")
filePath.set(file_selected)
print(filePath.get())
entry1.insert(0, filePath.get())
You can refer to https://www.geeksforgeeks.org/how-to-set-the-default-text-of-tkinter-entry-widget/ .

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

Python GUI text editor

I have a GUI text editor i made with tkinter. It currently opens, saves and clears the screen. I have added an encrypt and decrypt button and will eventually apply the ceasar cipher to whatever text is on the screen.
For my encrypt button i so far have
def encrypt():
text = editor.get(1.0, tk.END)
encryptList = []
for word in text:
encryptList.append(word)
random.shuffle(encryptList)
return encryptList
What i am trying to have happen is to shuffle the text on my screen from whatever i have on there (im using a test file with "Test" printed a bunch of times) After I'm able to do this I can implement a cesar cipher to the text on the screen. (I hope)
whole code:
#Import Module
import random
import tkinter as tk
from tkinter.filedialog import askopenfilename, asksaveasfilename
from tkinter import ttk
#Create Function for New File Button
def fileNew():
editor.delete(1.0, tk.END)
#Create Function For Open File Button
def fileOpen():
editor.delete(1.0, tk.END)
filepath = askopenfilename(filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
with open(filepath, "r") as file:
text = file.read()
editor.insert(tk.END, text)
def encrypt():
text = editor.get(1.0, tk.END)
encryptList = []
for word in text:
encryptList.append(word)
random.shuffle(encryptList)
return encryptList
#Create Function For Save File Button
def fileSave():
save = asksaveasfilename(defaultextension=".*", filetypes=[("Text Files", "*.txt"), ("All Files", "*.*")])
if not save:
return
with open(save, "w") as fileOutput:
text = editor.get(1.0, tk.END)
fileOutput.write(text)
#GUI
root = tk.Tk()
root.rowconfigure(0, minsize=500, weight=1)
root.columnconfigure(1, minsize=800, weight=1)
#Title
root.title()
#Text Editor
editor = tk.Text(root)
editor.configure(background="#263D42")
editor.grid(row=0, column=1, sticky="nsew")
#Scrollbar
scrollbar = ttk.Scrollbar(root, orient='vertical', command=editor.yview)
scrollbar.grid(row=0, column=2, sticky='ns')
#Left Side Panel
sidePanel = tk.Frame(root)
sidePanel.configure(background="#232e3a")
sidePanel.grid(row=0, column=0, sticky="ns")
#Open File Button
btn_open = tk.Button(sidePanel, text="Open", command=fileOpen)
btn_open.configure(background="#867c91")
btn_open.grid(row=0, column=0, sticky="ew", padx=5, pady=5)
#New File Button
btn_new = tk.Button(sidePanel, text="New", command=fileNew)
btn_new.configure(background="#867c91")
btn_new.grid(row=1, column=0, sticky="ew", padx=5, pady=5)
#Encrypt File Button
btn_open = tk.Button(sidePanel, text="Encrypt", command=encrypt)
btn_open.configure(background="#867c91")
btn_open.grid(row=2, column=0, sticky="ew", padx=5, pady=5)
#Decrypt File Button
btn_open = tk.Button(sidePanel, text="Decrypt")
btn_open.configure(background="#867c91")
btn_open.grid(row=3, column=0, sticky="ew", padx=5, pady=5)
#Save Button
btn_saveas = tk.Button(sidePanel, text="Save As", command=fileSave)
btn_saveas.configure(background="#867c91")
btn_saveas.grid(row=4, column=0, sticky="ew", padx=5, pady=5)
root.mainloop()
Closed. I figured it out
def encrypt():
text = editor.get(1.0, tk.END)
encryptList = []
for word in text:
encryptList.append(word)
random.shuffle(encryptList)
needed to add
editor.delete(1.0, tk.END)
editor.insert(tk.END, encryptList)

alright this is weird 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:

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