Python 'module' object is not callable with Font P - python

import os.path
from tkinter import *
from tkinter.ttk import *
import tkinter.filedialog
import tkinter.font as Font
try:
from tkinter.ttk import Button, Scrollbar
except ImportError:
pass
class Edit_save(object):
def __init__(self):
self.root=Tk()
self.root.title('EditSave')
self.root.geometry('+120+50')
self.font_en = Font(self.root, size=12)
self.font_text = Font(self.root,family="Helvetica",size=12,weight='normal')
self.menubar = Menu(self.root, bg='purple')
self.filemenu = Menu(self.menubar)
My code is like that, the error message is
'module' object is not callable.
The question is self.font_en = Font(self.root, size=12), Font is not callable. How should I solve the problem?
Thanks very much! And I am using Python 3.6.1

from tkinter.font import Font
I figure out what the problem is, it should be imported this way.

Related

Difficulty importing ThemedTK from ttkthemes

I'm trying to import ThemedTK from ttkthemes in Python3 but am getting the following error message:
line 4, in
from ttkthemes import Themed_TK
ImportError: cannot import name 'Themed_TK' from 'ttkthemes'
Any ideas?
from tkinter import filedialog
from tkinter import ttk
from ttkthemes import ThemedTK
from reportlab.lib.units import mm
from draw import bellGen
root = ThemedTK()
Apparently it's ThemedTk. With lowercase "k".
Here's the documentation for it: ttkthemes.readthedocs.io/en/latest/example.html

Error 'Image' has no attribute 'open' but it should

Im using PIL and TKinter to open an image. I do not understand why I'm getting this error
import os
import random
from PIL import Image
import time
from tkinter import *
root = Tk()
def timer(mins):
time.sleep(mins * 60)
def anmuViewer():
random_pic = (random.choice(os.listdir("D:/de_clutter/memez/anmu")))
openPic = Image.open('D:/de_clutter/memez/anmu/' + random_pic)
openPic.show()
timer(3)
start_btn = Button(root, text = "Start", command = anmuViewer)
start_btn.pack()
root.mainloop()
what should happen is a tkinter window should pop up with only a button called "start". When I click that button, a new window with the image should pop up. instead I get this error
line 17, in anmuViewer
openPic = Image.open('D:/de_clutter/memez/anmu/' + random_pic)
AttributeError: type object 'Image' has no attribute 'open'
Like Michael Butscher said,
"tkinter.Image" overwrites "PIL.Image" in your module's namespace. Avoid imports with *
But if you must use a wildcard import, importing tkinter before PIL should solve your problem
from tkinter import *
import os
import random
from PIL import Image
import time

Creating a UI for a Python Program. Theming with ttk themes for tkinter but it doesn't work for labels and buttons and returns a strange error

Here is the code written so far... The code basically functions as a UI for another Python program. The other python program isn't causing any trouble...
No one has been able to assist me with the previous post so I rephrased and reposted...
import tkinter as tk
from tkinter import ttk
from ttkthemes import themed_tk as tk
import subprocess
import sys
import time
import os
import tkinter.font as font
from tkinter.ttk import *
app = tk.ThemedTk()
app.get_themes()
app.set_theme("radiance")
app.geometry("400x400")
app.configure(bg='gray')
ex_activate_photo = tk.PhotoImage(file=r"C:\Users\bedga\PycharmProjects\GUIdev\ex_button_active.png") #It underlines PhotoImage
myFont = font.Font(family='Helvetica', size=20, weight='normal')
ttk.Label(app, text='Ex', bg='gray', font=(
'Verdana', 15)).pack(side=tk.TOP, pady=10)
app.iconbitmap(r'C:\Users\ex\ex_icon.ico')
def ex_activation():
global pro
print("Ex")
pro = subprocess.Popen("python ex.py", shell=True)
def ex_stop():
global pro
print("Stopping Program... Please Wait!")
os.kill(pro.pid, 0)
ex_activation_button = ttk.Button(app, bg='black', image=ex_activate_photo, width=120, height=120, command=ex_activation)
ex_stop_button = ttk.Button(app, bg='Gray', text='Stop Program', width=12, command=ex_stop, height=3)
ex_stop_button['font'] = myFont
app.title("Ex")
ex_activation_button.pack(side=tk.TOP)
ex_stop_button.pack(side=tk.LEFT)
# app.mainloop()
while True:
try:
app.update()
app.update_idletasks()
except KeyboardInterrupt:
pass
The goal here is to ultimately theme every button (2) and the label at the top. I can then apply similar methods when theming new things in the future. Currently, the PhotoImage is not liking tk and ttk. The program underlines it. One of the buttons being themed is photo-based and the other is text. I have seen successful projects with themed image buttons.
This is the error I get with tk.photoimage
Traceback (most recent call last):
File "C:/Users/ex/main.py", line 19, in <module>
ex_activate_photo = tk.PhotoImage(file=r"C:\Users\ex\ex_button_active.png") #It underlines PhotoImage
AttributeError: module 'ttkthemes.themed_tk' has no attribute 'PhotoImage'
EDIT: This is the error I get for doing
import tkinter as tk
from ttkthemes import themed_tk as tkk
import subprocess
import sys
import time
import os
import tkinter.font as font
from tkinter.ttk import *
I get this error:
Traceback (most recent call last):
File "C:/Users/ex/main.py", line 19, in <module>
ex_activate_photo = tk.PhotoImage(file=r"C:\Users\ex\ex_button_active.png") #It underlines PhotoImage
File "C:\Users\ex\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 3539, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\ex\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 3495, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "C:\Users\ex\PycharmProjects\ex\ex_button_active.png": no such file or directory
I didn't think ttk themes would have an issue with PhotoImage as a variable because it is a theming library for tkinter.
I am very new the GUI development in Python and any help greatly appreciated.
You are importing 2 libraries as tk, that's your main problem. First 3 lines of your code is here
import tkinter as tk
from tkinter import ttk
from ttkthemes import themed_tk as tk
First and third lines have as tk so the latest one is taking over. The error message points to this as well. You should rename one of them.

Tkinter MessageBox gives error

In a GUI program written in Python 3.5 I use the Tkinter module. I define a function to call the MessageBox as follows:
def callAbout():
messagebox.showinfo(title = "About", message = "My Window")
When I try to execute, I get the following error message:
Exception in Tkinter callback
Traceback (most recent call last):
[PATH]
return self.func(*args)
File "tk-error.py", line 4, in callAbout
messagebox.showinfo(title = "About", message = "My Window")
NameError: name 'messagebox' is not defined
It seems that the program cannot find
messagebox
but I wonder why, since I imported the whole module with
from tkinter import *
Please, ask if you need the complete code.
Thanks in advance for your help.
Added: Here follows the whole code.
from tkinter import *
def callAbout():
messagebox.showinfo(title = "About", message = "My Window")
win = Tk()
win.geometry('300x300')
win.title("My First Window")
bar_menu = Menu(win)
menu_about = Menu(bar_menu, tearoff = 0)
bar_menu.add_cascade(label = "About", menu = menu_about)
menu_about.add_command(label = "About", command = callAbout)
win.config(menu = bar_menu)
win.mainloop()
You can import and use the messagebox module by using an alias:
import tkinter.messagebox as tkmb
Here is your code after making changes,
import tkinter as tk
import tkinter.messagebox as tkmb
def callAbout():
tkmb.showinfo(title = "About", message = "My Window")
win = tk.Tk()
win.geometry('300x300')
win.title("My First Window")
bar_menu = tk.Menu(win)
menu_about = tk.Menu(bar_menu, tearoff = 0)
bar_menu.add_cascade(label = "About", menu = menu_about)
menu_about.add_command(label = "About", command = callAbout)
win.config(menu = bar_menu)
win.mainloop()
I had same problem.
I changed the code like this. simply...
from tkinter import *
from tkinter import messagebox
messagebox, along with some other modules like filedialog, does not automatically get imported when you import tkinter. Import it explicitly, using as and/or from as desired. check below 3 examples for better clarification-
>>> import tkinter
>>> tkinter.messagebox.showinfo(message='hi')
Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute 'messagebox'
.
>>> import tkinter.messagebox
>>> tkinter.messagebox.showinfo(message='hi')
'ok'
.
>>> from tkinter import messagebox
>>> messagebox.showinfo(message='hi')
'ok'
Change messagebox.showinfo to showinfo and add from tkinter.messagebox import showinfo
from tkinter import *
from tkinter.messagebox import showinfo
def callAbout():
showinfo(title="About", message="My Window")
win = Tk()
win.geometry('300x300')
win.title("My First Window")
bar_menu = Menu(win)
menu_about = Menu(bar_menu, tearoff=0)
bar_menu.add_cascade(label="About", menu=menu_about)
menu_about.add_command(label="About", command=callAbout)
win.config(menu=bar_menu)
win.mainloop()
Output:

tk messagebox import confusion

I'm just beginning to learn tkinter at the moment, and when importing messagebox I found that I must not really understand import statements.
The thing that confuses me is that:
import tkinter as tk
def text_box():
if tk.messagebox.askokcancel("Quit", "Never Mind"):
root.destroy()
root = tk.Tk()
button = tk.Button(root, text="Press the button", command=text_box)
button.pack()
root.mainloop()
compiles fine, but pressing the button gives the error 'module' object has no attribute 'messagebox', while the code:
import tkinter as tk
from tkinter import messagebox
...
if messagebox.askokcancel("Quit", "Never Mind"):
...
...works without a hitch.
I get a similar error if I import with from tkinter import *.
The help for tkinter shows messagebox in the list of PACKAGE CONTENTS, but I just can't load it in the normal way.
So my question is, why...and what is it about importing that I don't understand?
Just thought I should mention—the code only works in Python 3, and in Python 2.x messagebox is called tkMessageBox and is not defined in tkinter.
tkinter.messagebox is a module, not a class.
As it isn't imported in tkinter.__init__.py, you explicitly have to import it before you can use it.
import tkinter
tkinter.messagebox # would raise an ImportError
from tkinter import messagebox
tkinter.messagebox # now it's available eiter as `messagebox` or `tkinter.messagebox`
try this
import sys
from tkinter import *
... and your code

Categories

Resources