Python Forum

Full Version: def functions in tkinter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need to do something like this:
from tkinter import *
import tkinter as tk
from PIL import ImageTk, Image
import tkinter.filedialog

class ST:
    def __init__(self, char):
        self.ST = char
        self.F = Frame(char)
        self.F.place(x = 0, y = 0, width=460, height=401)

        #def
        def s():
            self.s = Canvas(self.ST)
            self.s.configure(bd=0, highlightthickness=0)
            self.s.place(x=0, y=0, width=460, height=401)
            self.s.create_image(0, 0, image=self.image, anchor=NW)
            self.s.create_text(220, 80,fill="black",font="Times 20 italic bold", text="Skill")

        s = s()

        def c():
            self.c = Canvas(self.ST)
            self.c.configure(bd=0, highlightthickness=0)
            self.c.place(x=0, y=0, width=460, height=401)
            self.c.create_image(0, 0, image=self.image, anchor=NW)
            self.c.create_text(220, 80,fill="black",font="Times 20 italic bold", text="Anne Bonny")
            self.c.create_text(220, 120,fill="black",font="Times 12 italic bold", text="   20 years old \n Loyal to Portugal")

        #Background
        self.image = PhotoImage(file="D:\\o.gif")
        self.bg = Canvas(self.ST)
        self.bg.place(x=0, y=0, width=460, height=401)
        self.bg.create_image(0, 0, image=self.image, anchor=NW)


        #Character
        self.c()

        self.imgc = ImageTk.PhotoImage(Image.open('D:\\f.png'))
        self.panelc = Label(self.ST, image = self.imgc)
        self.panelc.place(x = 59, y = 55, width=75, height=89)

        self.ST.bind("f", self.skill)
        
    def skill(self, event):
        self.panelc.place_forget()
        self.c.place_forget()
        s().place(x=0, y=0, width=460, height=401)        
        
        
char = Tk()
st = ST(char)
char.geometry("460x401")
char.wm_attributes("-transparentcolor", "yellow")
char.mainloop()
when the f key was pressed it should call def s (), but it's not working, can anyone tell me why?

show
NameError: name 's' is not defined
Before your edit the indentation was off. Is it like that in the file? I saw that as the problem.
(Dec-13-2019, 08:55 PM)michael1789 Wrote: [ -> ]Before your edit the indentation was off. Is it like that in the file? I saw that as the problem.
(Dec-13-2019, 08:55 PM)michael1789 Wrote: [ -> ]Before your edit the indentation was off. Is it like that in the file? I saw that as the problem.
This program is more complete, and it is correctly indented.
Full disclosure, I don't know anything about tkinter specifically, but to my eyes your "s = s()" and your Background and Character stuff are all on the level that gets read in as your ST.__init__.

And "s = s()" sets the value of "s" to "s()", it doesn't call the function. I think, anyway.
I substituted your photos and ran your script- it only outputs errors so I started to comment out lines. I finally got an gui with removing:
  • #s = s()
  • #self.c()
  • #char.wm_attributes("-transparentcolor", "yellow")
the background image showed but the label got garbage collected.
Think about starting over and here's a couple tips:
You have imported tkinter twice once as a wild card * and as tk use the tk.
so your reference will look like self.canvas= tk.Canvas(...)
keep it simple with one canvas object. You can change it's color or clear it-
self.canvas.delete('all') to clear it
self.canvas.itemconfig(bg='black)
you don't need to create new a canvas.
remove the functions in the init function and create them like your skill function.
when you call a function
Quote:s().place(x=0, y=0, width=460, height=401)
have your function widget place or pack or grid the object
I like to recommend the guizero module to people starting with tkinter the creators have
made it easy and simplified the process to making a gui
you can find the docs here:guizero docs
here's an example of your code converted to guizero use your own paths to images:
from guizero import (Picture,App,PushButton,Text)
def change_background():
    picture_1.hide()
    picture_2.hide()
    app.bg='black'
    txt_1.value='Hello Iowa'
    txt_1.color='white'
def redo_background(event):
    picture_1.show()
    picture_2.show()
    app.bg='white'
    txt_1.value='you pressed a key'
    txt_1.color= 'purple'

app= App(title='my app',height=600,width=600)
btn1= PushButton(app, image='path_to_image',
                 height=100,width=100,align='top',
                 command= change_background)
picture_1= Picture(app, image='path_to_image')
picture_2= Picture(app, image='path_to_image',align='left',
                   height=100,width=100)
txt_1= Text(app, text='Hello World even Iowa', color='green',
                size=16, align='bottom')
app.tk.bind('f', redo_background)

app.display()
    
it's a wrapper of tkinter so you'll be familiar with the function it accepts png and gif files by
default or you can use PIL.
best of luck,
Joe
None of your functions are members of the class so
        def s():
            self.s = Canvas(self.ST)
does not know what self refers to.