import tkinter as tk
from pynput.mouse import Button, Controller
from tkinter import *
from tkinter import ttk
import pyautogui
root = tk.Tk()
root.attributes('-fullscreen', True)
root_felk = tk.Toplevel(root)
root_felk.title("Feladat kártya kiválasztása")
root_felk.grid()
def abllét():
top_windows = tk.Toplevel(root)
top_windows.title("Játékszabály")
a = Canvas(top_windows, width =1200, height =900, bg ='grey', bd =2, relief =SOLID,scrollregion=(0,0,0,0))
photo = PhotoImage (file ='Szabály fekvő EREDETI MÉRET.gif')
item = a.create_image(580, 350, image=photo,tags='ed')
a.grid(row =1, column =1, rowspan=6, padx= 0, pady= 5)
grid() # this is it !!!!!!!!!!!!!!!!
my_menu = Menu(root)
root.config(menu=my_menu)
file_menu = Menu(my_menu, tearoff=0)
my_menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Szabály", command=abllét)
file_menu.add_command(label="Exit", command=root.quit)
root.mainloop()
Please Tell us what’s happening in your own words.
Learning to describe problems is hard, but it is an important part of learning how to code.
Also, the more you say, the more we can help!
The problem is that if I don’t write line 23, (pack()) which makes no sense), the image doesn’t appear on the screen. If I type , it appears, but IDLE gives this error:
Exception in Tkinter callback
Traceback (most recent call last):
File “C:\Users\renat\AppData\Local\Programs\Python\Python311\Lib\tkinter_init_.py”, line 1948, in call
return self.func(*args)
^^^^^^^^^^^^^^^^
File “C:/XboxGames/Jatek/feltöl.py”, line 22, in abllét
grid()
^^^^
NameError: name ‘grid’ is not defined
After the def, the lines are indented by 4 spaces up to the get() method, but the code is not displayed correctly
If you’re wanting to display an image, this is probably the easiest.
The best way to write code is to use an editor and not idle, A lot of people use pycharm or notepad++, and vscode. My preference is vscode.
It is not recommended to do wildcard imports e.g. **from tkinter import *** . This could cause unwanted results. I dont know the extent of your code but, only import modules you need.
import tkinter as tk
root = tk.Tk()
root.attributes('-fullscreen',True)
image = tk.PhotoImage(file='path/to/my/image.png')
label = tk.Label(root, image=image)
label.pack()
# Button to close the fullscreen window
btn = tk.Button(root, text='Close Window', command=root.destroy)
btn['font'] = (None, 14, 'normal')
btn['bg'] = 'tomato'
btn['activebackground'] = 'red'
btn['activeforeground'] = 'white'
btn['cursor'] = 'hand2'
btn.pack()
root.mainloop()
Same result. In this case, until I write a pack() , the image is also not displayed, only a gray background. I enter, then it appears
Are you trying to open a toplevel window that displays an image?
Yes. I want to start the window from the menu bar.
Maybe something like this
import tkinter as tk
# Define a function for opening a top level window
def top_window():
window = tk.Toplevel(None)
window.title('Top Level Window')
window.geometry('200x200')
# Get image using tk.PhotoImage
image = tk.PhotoImage(file='path/to/my/image.png')
# Need a reference to the image due to garbage collection
image.backup = image
# Display image within a label
label = tk.Label(window, image=image)
label.pack(expand=True, fill='both')
# Button to close toplevel window
btn = tk.Button(window, text='close', command=window.destroy)
btn.pack()
root = tk.Tk()
# Initiate menu
menubar = tk.Menu(root)
# Configure root window
root.config(menu=menubar)
# Define menu
file_menu = tk.Menu(menubar, tearoff=False)
# Add menu options and commands
file_menu.add_command(label='Open Window', command=top_window)
file_menu.add_command(label='Exit', command=root.destroy)
# Display the menu options
menubar.add_cascade(label='File', menu=file_menu)
root.mainloop()
Thanks for help., no its work fine