I have constructed an application, the source starts like this:
from tkinter import Text
from tkinter import Label
from AESEncDec import *
from MD5Hashing import *
from RSAEncDec import *
color = 'lightblue' #color our background
class Application(Frame):
def __init__(self, root=None):
Frame.__init__(self, root)
self.frame_width = 700
self.frame_height = 400
But last piece of it cannot execute:
#create object TK class
the_window = Tk(className = " Cryptographic")
#create object Application
app = Application(the_window)
#run our Application
app.mainloop()
And it gives the NameError:
Traceback (most recent call last):
File "/home/artur/Documents/MScProject/MSc Project/Task #179276/main_program.py", line 169, in
the_window = Tk(className = " Cryptographic")
NameError: name 'Tk' is not defined
How should I define it properly in this case?
You miss an import statement : from tkinter import Tk
The best way to avoid conflict, is to import the whole module, eventually with an alias to make it short (but don't forget to add tk. everywhere you've called a tkinter widget):
import tkinter as tk
from AESEncDec import *
from MD5Hashing import *
from RSAEncDec import *
color = 'lightblue' #color our background
class Application(tk.Frame):
def __init__(self, root=None):
tk.Frame.__init__(self, root)
self.frame_width = 700
self.frame_height = 400
#create object TK class
the_window = tk.Tk(className = " Cryptographic")
#create object Application
app = Application(the_window)
#run our Application
app.mainloop()
Related
I am able to get the image to display in the GUI with Tkinter however I can not get this part of the code to work as I am trying to keep it all either under one .py file because it is easier for me to keep everything in one singular window.
The error I am getting is:
Traceback (most recent call last):
File "C:/Users/Holcomb Family/PycharmProjects/webscrapeTKINTER/venv/main4.py", line 32, in <module>
run = MainWin(root)
pil_img = Image.open(my_picture)
AttributeError: type object 'Image' has no attribute 'open'
My code:
import io
from PIL import Image, ImageTk
from tkinter import *
from urllib.request import urlopen
root = tk.Tk()
root.title("New Window")
class MainWin:
def __init__(self, master):
self.master = master
url1 = "https://img.finalfantasyxiv.com/lds/pc/global/images/itemicon/"
url2 = "da/dad2c1875e87e7a7f1d02fce24f0bd0351cdda11.png?n5.45"
pic_url = url1 + url2
my_page = urlopen(pic_url)
my_picture = io.BytesIO(my_page.read())
pil_img = Image.open(my_picture)
tk_img = ImageTk.PhotoImage(pil_img)
self.label = tk.Label(root, image=tk_img)
self.label.pack(padx=5, pady=5)
root = Tk()
run = MainWin(root)
root.mainloop()
I want to import tkFont but it's not working
from tkinter import *
import tkFont
class BuckysButtons:
def __init__(self,master):
frame = Frame(master)
frame.pack()
helv36 = tkFont.Font(family="Helvetica",size=36,weight="bold")
self.printButton = Button(frame,font=helv36, text ="Print
Message",command = self.printMessage,compound ='top')
self.printButton.pack(side =LEFT)
self.quitButton = Button(frame, text ="quit", command = frame.quit)
self.quitButton.pack(side=LEFT)
def printMessage(self):
print("It worked!")
root = Tk()
b = BuckysButtons(root)
root.mainloop()
i'm getting following error:
Traceback (most recent call last):
File "exercise.py", line 2, in <module>
import tkFont
ModuleNotFoundError: No module named 'tkFont'
It's possible you are trying to run Python 2 code under Python 3, which did some library reorganisation.
If you replace your current import with import tkinter.font as TkFont that should suffice to move you forward.
I have made my own widget called 'InventorySlot' and I need the widgets within my custom one to be made onto a Canvas instead of using 'grid' or 'pack'.
import tkinter as tk
from tkinter import *
from tkinter.ttk import *
class Main:
def __init__(self):
self.root = Tk()
self.root.geometry('500x500')
self.test = InventorySlot(self.root)
self.test.grid()
class InventorySlot(tk.Frame):
def __init__(self,parent,*args,**kwargs):
tk.Frame.__init__(self,parent)
self.options = {}
self.options.update(kwargs)
self.slot = tk.Label(self,height=3,width=6,text='',relief=SUNKEN,bg='#8b8b8b',bd=4,padx=1,pady=0)
self.canvas = Canvas(self)
self.canvas.create_window(10,10,window=self.slot)
self.canvas.grid()
MainTk = Main()
MainTk.root.mainloop()
All it shows is a blank Canvas
You need to create the label after creating the canvas. The order of creation determines the stacking order (ie: the z-index). The label on s there, it’s just behind the canvas.
I saw the sorting example in turtle demo which python includes and I would like to add similar animations in my program. My program is based in tkinter and I would like to insert turtle animations in a tkinter canvas (with RawTurtle) so first I tried to create a black box in the canvas and I get the following error message:
AttributeError: 'RawTurtle' object has no attribute 'Turtle'
Here's my code:
import tkinter
from turtle import *
class MyApp():
def __init__(self, parent):
self.p = parent
self.f = tkinter.Frame(self.p).pack()
self.c = tkinter.Canvas(self.f, height = '640', width = '1000')
self.c.pack()
self.t = RawTurtle(self.c)
self.main(5)
def main(self, size):
self.t.size = size
self.t.Turtle.__init__(self, shape="square", visible=False)
self.t.pu()
self.t.shapesize(5, 1.5, 2)
self.t.fillcolor('black')
self.t.st()
if __name__ == '__main__':
root= tkinter.Tk()
frame = MyApp(root)
root.mainloop()
You pretty much have it -- those two settings you were trying to change via the non-existent Turtle() instance method can be handled when creating the RawTurtle:
import tkinter
from turtle import RawTurtle
class MyApp():
def __init__(self, parent):
self.p = parent
self.f = tkinter.Frame(self.p).pack()
self.c = tkinter.Canvas(self.f, height=640, width=1000)
self.c.pack()
self.t = RawTurtle(self.c, shape='square', visible=False)
self.main(5)
def main(self, size):
self.t.size = size # does nothing if stamping with pen up
self.t.penup()
self.t.shapesize(5, 1.5, 2)
self.t.fillcolor('black') # the default
self.t.stamp()
if __name__ == '__main__':
root = tkinter.Tk()
frame = MyApp(root)
root.mainloop()
Hey I'm following a tutorial, https://www.youtube.com/watch?v=a1Y5e-aGPQ4 , and I can't get it to work properly. I'm trying to add an image when you press on a menu button:
from tkinter import *
from PIL import *
class Window(Frame):
def __init__(self, master = None):
Frame.__init__(self, master)
self.master = master
self.init_Window()
def init_Window(self):
self.master.title("GUI")
self.pack(fill =BOTH, expand=1)
#quitButton = Button(self, text = "Quit", command = self.client_exit)
#quitButton.place(x=0,y=0)
menu = Menu(self.master)
self.master.config(menu=menu)
file=Menu(menu)
file.add_command(label='Save',command= self.client_exit)
file.add_command(label='Exit',command= self.client_exit)
menu.add_cascade(label='File',menu=file)
edit = Menu(menu)
edit.add_command(label='Show Image', command=self.showImg)
edit.add_command(label='Show Text', command=self.showTxt)
menu.add_cascade(label='Edit',menu=edit)
def showImg(self):
load = Image.open('Pic.png')
render = ImageTk.PhotoImage(load)
img = Label(self, image=render)
img.image = render
img.place(x=0,y=0)
def showTxt(self):
text = Label(self,text='Hey')
text.pack
def client_exit(self):
exit()
root = Tk()
root.geometry("400x300")
app = Window(root)
root.mainloop()
I have tried asking around school, StackOverflow, and YouTube for about 3 days now, and nothing has solved my problem, if you need any more info about it please ask. I am getting the error code:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.5/tkinter/__init__.py", line 1558, in __call__
return self.func(*args)
File "/root/Desktop/Python Programs/Tkinter.py", line 35, in showImg
load = Image.open('pic.png')
AttributeError: type object 'Image' has no attribute 'open'
You use import * so you don't know if you use tkinter.Image or PIL.Image . And this is why you shouldn't use import *
Try
from PIL import Image, ImageTk
Images are a bit tricky to get right, some tips: Keep the image object global, to avoid garbage collection, avoid Attribute Error (by reading the docs).
In this example I don t use PIL and I load a gif image
#!/usr/bin/python
#-*-coding:utf-8 -*
#Python 3
#must have a valid gif file "im.gif" in the same foldeer
from tkinter import *
Window=Tk()
ListePhoto=list()
ListePhoto.append(PhotoImage(file="im.gif"))
def Try():
Window.title('image')
Window.geometry('+0+0')
Window.configure(bg='white')
DisplayImage()
def DisplayImage():
label_frame=LabelFrame(Window, relief='ridge', borderwidth=12, text="AnImage",
font='Arial 16 bold',bg='lightblue',fg='black')
ListeBouttons=list()#Liste Vide pour les Radiobutton(s)
RadioButton = Radiobutton(label_frame,text="notext",image=ListePhoto[0], indicatoron=0)
RadioButton.grid(row=1,column=1)
label_frame.pack(side="left")
if __name__ == '__main__':
Try()