Problem with a simple code to open windows file explorer on Pycharm

40 Views Asked by At

I try to make a little program wit Pycharm, following some tutorials. But i get this error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Anton1n\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 1967, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\Anton1n\PycharmProjects\Livechatvideo\Base.py", line 5, in open_file
    file_path = filedialog.askopenfilename()
                ^^^^^^^^^^
NameError: name 'filedialog' is not defined

with this code:

import tkinter as tk
from tkinter import *
from tkinter import filedialog

def open_file():
    filename = filedialog.askopenfilename(initialdir = "/",
                                          title = "Select a File",
                                          filetypes=(("PNG", "*.png"),
                                           ("JPEG", "*.jpg;*.jpeg"),
                                           ("GIF", "*.gif") ))

    send_button.configure(text="File Opened: " + filename)

# Créer une fenêtre
root = Tk()

# Personnaliser la fenêtre
root.title("LivechatVideo")
root.geometry("300x300")
root.minsize(350, 400)
root.iconbitmap("livechatico.ico")
root.config(bg="#4279B4")

# Créer la frame
frame = Frame(root, bg="#4279B4", bd=1, relief=SUNKEN)

# Ajouter un premier texte
label_title = Label(frame, text="Bienvenue sur LivechatVideo", font=("calibri", 15), fg="orange", bg="#4279B4")
label_title.pack()

# Ajouter second texte
label_subtitle = Label(frame, text="Envoyez vos photos ou vidéos directement à vos amis", font=("calibri", 10), fg="white", bg="#4279B4")
label_subtitle.pack()

# Ajouter
frame.pack(pady=5)

# Ajout de widgets (boutons, labels, etc.) à la fenêtre
send_button = Button(root, text="Sélectionnez votre fichier", font=("calibri", 10), bg="white", fg="black", command=open_file)
send_button.pack(pady=15)

# Création barre de menu
menu_bar = Menu(root)
# Création d'un menu
file_menu = Menu(menu_bar, tearoff=0)
file_menu.add_command(label="Option")
menu_bar.add_cascade(label="Fichier", menu=file_menu)

# Configuraiton de la fenêtre pour menu
root.config(menu=menu_bar)

# Afficher
root.mainloop()

I get the error when i launch the program and click on the button "Sélectionnez votre fichier". I tried almost everything I found on internet, and every way to import tkmodule. I use Python 3. I simply want to open a windows file explorer to select an image on my computer. Why can't it work?

0

There are 0 best solutions below