I run this code which aim is only to display a menu bar.
There is a simple menu bar in which 3 sub-menus are created without performing anything except for the Exit one which closes the window:
from Tkinter import *
import tkMessageBox
import numpy as np
import ttk
import tkFont
from PIL import ImageTk, Image
from tkColorChooser import askcolor
from tkFileDialog import askopenfilename
class MyGui(Frame):
def __init__(self,master):
Frame.__init__(self,master)
self.master=master
self.themenus() # menu initialization within the constructor
def themenus(self):
self.menubar=Menu(self.master)
self.filemenu=Menu(self.menubar,tearoff=0)
self.filemenu.add_command(label="Open Image")
self.filemenu.add_command(label="Save Image")
self.filemenu.add_command(label="Exit",command=self.master.quit)
self.menubar.add_cascade(label="File",menu=self.filemenu)
if __name__=="__main__":
root=Tk()
root.wm_title("Test")
mg=MyGui(root)
root.mainloop()
I get this error:
libdc1394 error: Failed to initialize libdc1394
How to fix this ?
EDIT:
I resolved the problem by removing away the original imports that I did not use after all:
import tkMessageBox
import numpy as np
import ttk
import tkFont
from PIL import ImageTk, Image
from tkColorChooser import askcolor
from tkFileDialog import askopenfilename
Now, no error is triggered, however I do not see the menu displayed. Why ?
I resolved my problem by adding this line at the end of themenus() function:
self.master.config(menu=self.menubar)
Related
Attribute Error: type object 'Image' has no attribute 'open'
this code is giving this error
from tkinter import *
from tkinter import messagebox
from PIL import ImageTk
\#from PIL import Image
import PIL.Image
root=Tk()
root.geometry('300x400')
Button(root,text='open second window',command=open).pack()
def open():
global myimage , img
img=PIL.Image.open("C:\\Users\\HP\\Desktop\\test\\img_lights.jpg")
myimage = ImageTk.PhotoImage(img)
top=Toplevel()
top.geometry("300x400")
Button(top,text='close window',command=top.destroy).pack()
Label(top,image=myimage).pack()
mainloop()
I want the image to come on top level window but it is showing attribute error
I do not used PIL. Re-arranged your code in order to make it working. Don't use double slash.
Code:
from tkinter import *
from tkinter import messagebox
from PIL import ImageTk
import PIL.Image
root=Tk()
root.geometry('300x400')
def open():
global myimage , img
img=PIL.Image.open(r"C:\Users\HPDesktop\test\img_lights.jpg")
myimage = ImageTk.PhotoImage(img)
top=Toplevel()
top.geometry("300x400")
Button(top,text='close window',command=top.destroy).pack()
Label(top,image=myimage).pack()
Button(root,text='open second window',command=open).pack()
root.mainloop()
Output:
I'm trying to add the ScrolledText widget to a Tkinter window. The program reads it perfectly as in it accepts the INSERT method for it with no errors but it's not showing up. The problem came up when I added Notebook Tabs. I've attached the code snippet. I used the place() method because I need the rest of my buttons and labels arranged in a specific pattern.
import tkinter
from tkinter import *
from tkinter import scrolledtext
from tkinter import messagebox
from tkinter import ttk
import os
import datetime
# Variables
window = Tk()
window.title("Vesnica Pomenire")
window.geometry('1500x1000')
var = IntVar()
var.set(1)
txt = scrolledtext.ScrolledText(window,width=40,height=10)
txt.place(x=50, y=50)
You're missing the mainloop()
import tkinter
from tkinter import *
from tkinter import scrolledtext
from tkinter import messagebox
from tkinter import ttk
import os
import datetime
# Variables
window = Tk()
window.title("Vesnica Pomenire")
window.geometry('1500x1000')
var = IntVar()
var.set(1)
txt = scrolledtext.ScrolledText(window,width=40,height=10)
txt.place(x=50, y=50)
window.mainloop() #You are missing this
You can read more about mainloop() here
You really missed mainloop command
window.mainloop()
add this at the bottom of your code and it will do the thing
I'm working on this project and I deiced to add a GUI interface. I have chosen to work with Tkinter cause I'm some sort familiar with Python. I'm running into the problem where I can run the GUI out of visual studio but I am unable to run the GUI off straight of my desktop. I have checked and there are no errors in my code. Can someone please help me fix the code.
This Is a snippet of the code which I am using to run the GUI
from tkinter import *
from tkinter.ttk import Progressbar
from tkinter import ttk
from tkinter import messagebox
import os
import shutil
from os import listdir
from os.path import isfile, join
import getpass
import time
window = Tk()
window.title("Move Files")
window.geometry('546x500')
def Movie():
TextBox.delete('1.0',END)
bar['value'] = 0
messagebox.showinfo('Message title', 'Message content')
def TVShow():
TextBox.delete('1.0',END)
bar['value'] = 0
TVShowMove()
#Buttons
Movie = Button(window,text='Move Movies', command=Movie, padx=50, pady=30)
Movie.place(x=40, y=40)
TVShow = Button(window,text='Move TV Shows', command=TVShow, padx=48, pady=30)
TVShow.place(x=300, y=40)
#Progressbar
bar = Progressbar(window, length=446, style='black.Horizontal.TProgressbar')
bar.place(x=40, y=140)
#TextBox
TextBox = Text(window, height=10, width=55)
TextBox.pack()
TextBox.place(x=40, y=170)
window.mainloop()
code is ok
i got this problem too!
try to open it from vscode itself
In Python 3.8 you cant import any of these.
from tkinter.ttk import Progressbar
from tkinter import ttk
from tkinter import messagebox
Instead, you have to import Tkinter as tk as seen below
from tkinter import *
import tkinter as tk
I am following this tutorial to create my own simple text editor. However, I am coming across an error I don't know how to fix. I'm running this code:
from tkinter import *
import tkinter.tkFileDialog
I've also tried this:
import tkinter
import tkinter.tkFileDialog
Both of them give me this error:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import tkinter.tkFileDialog
ModuleNotFoundError: No module named 'tkinter.tkFileDialog'
I'm doing exactly what the tutorial said to do, but it won't work. Why is this happening?
For reference, I am using python 3.7 64-bit on windows 10.
Python 3 tkinter does not have a tkFileDialog import. Instead you want to import filedialog like this.
import tkinter as tk # this is the preferred import for tkinter
from tkinter import filedialog
root = tk.Tk()
x = filedialog.askopenfilename()
print(x)
root.mainloop()
If you would prefer to only import the dialog's you specifically need you can do something like this.
import tkinter as tk # this is the preferred import for tkinter
from tkinter.filedialog import askopenfilename
root = tk.Tk()
x = askopenfilename()
print(x)
root.mainloop()
Update: Based on what Bryan has mentioned in the below here is another example that includes a delay to help prevent the issue mentioned. Though this only applies to dilogs opened before the mainloop has been reached and for many applications I would think this is not an issue as dialog is not often the first thing you have up in a GUI. However it is still good information to have.
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
def print_file_name():
x = filedialog.askopenfilename()
print(x)
root.after(100, print_file_name)
root.mainloop()
Or:
import tkinter as tk # this is the preferred import for tkinter
from tkinter.filedialog import askopenfilename
root = tk.Tk()
def print_file_name():
x = askopenfilename()
print(x)
root.after(100, print_file_name)
root.mainloop()
I am Trying to set an image as the background of my Tkinter window and everything I try doesn't seem to work. Here's the code I have:
import random
import Tkinter # note use of caps
from Tkinter import *
from PIL import Image, ImageTk
import os.path
window = Tk()
window.title('Ask If You Dare')
window.configure(background = '#007501')
If you want the background to be an image (without using canvas), use labels:
import random
import Tkinter # note use of caps
from Tkinter import *
from PIL import Image, ImageTk
import os.path
window = Tk()
window.title('Ask If You Dare')
photo=PhotoImage(file="path/to/your/file")
l=Label(window,image=photo)
l.image=photo #just keeping a reference
l.grid()