canvas command not working inside function

Help! I am using python 3.5.2 and the function self.new_game is not working. It is supposed to put text on the canvas but it does nothing! There are also no errors that appear in the shell.

from tkinter import *
import time
import os

WIDTH = 1920
HEIGHT = 1080
root = Tk()
root.state('zoomed')
planet_selected = 0
planet_name = "nothing"
planet_temp = -270
planet_size = 0.0
planet_life = 0.0

class Space(Frame):
    def __init__(self):
        Frame.__init__(self)
        frame1 = Frame(self)
        self.canvas = Canvas(frame1, width = WIDTH, height = HEIGHT, bg ="white")
        self.canvas.focus_set()
        self.canvas.create_text(1920,1000,text='Planetary Creator',font=('Arial',15))
        self.master.title("Planetary Creator Alpha 0.1")
        frame = Frame(root, bg='grey', width=1920, height=40)
        frame.pack(fill='x')
        button1 = Button(frame, text='New Game',command=lambda : self.new_game())
        button1.pack(side='left', padx=10)
        button2 = Button(frame, text='Quit Game',command=lambda : os._exit(0))
        button2.pack(side='left')

#this function below does not work!!!

    def new_game(self):
        self.canvas.delete(ALL)
        size = self.canvas.create_text(960,540,text=str(planet_size) + "moon",font=("Arial",10))
        life = self.canvas.create_text(960,520,text="✣" + str(planet_life) + "%",font=("Arial",10))
        temp = self.canvas.create_text(960,500,text=str(planet_temp) + "°C",font=("Arial",10))
        name = self.canvas.create_text(960,480,text=planet_name,font=("Arial",15))

Space().mainloop()

Upvotes: 0

Views: 254

Answers (1)

furas
furas

Reputation: 142641

I removed frame1 and put Canvas in root , and use canvas.pack() to see canvas in window.
(but I could use self instead of root and use self.pack() because Space inherits from Frame. it would ne more logical)

After that I had to only change text positions because windows was too big for my screen.

I used variables CENTER_X, CENTER_Y to put text in center regardless of the size of the screen.

from tkinter import *
import time
import os

class Space(Frame):

    def __init__(self, master):
        Frame.__init__(self, master)

        self.master.title("Planetary Creator Alpha 0.1")

        self.canvas = Canvas(root, width=WIDTH, height=HEIGHT, bg="white")
        self.canvas.pack()
        self.canvas.focus_set()

        self.canvas.create_text(CENTER_X, CENTER_Y, text='Planetary Creator', font=('Arial',15))

        frame = Frame(root, bg='grey', width=WIDTH, height=40)
        frame.pack(fill='x')

        button1 = Button(frame, text='New Game', command=self.new_game)
        button1.pack(side='left', padx=10)

        button2 = Button(frame, text='Quit Game', command=root.destroy)
        button2.pack(side='left')

    def new_game(self):
        self.canvas.delete(ALL)

        size = self.canvas.create_text(CENTER_X, CENTER_Y, text=str(planet_size) + "moon", font=("Arial",10))
        life = self.canvas.create_text(CENTER_X, CENTER_Y-20, text="✣" + str(planet_life) + "%", font=("Arial",10))
        temp = self.canvas.create_text(CENTER_X, CENTER_Y-40, text=str(planet_temp) + "°C", font=("Arial",10))
        name = self.canvas.create_text(CENTER_X, CENTER_Y-60, text=planet_name, font=("Arial",15))

# --- main ---

WIDTH = 800 #1920
HEIGHT = 500 #1080

CENTER_X = WIDTH//2
CENTER_Y = HEIGHT//2

planet_selected = 0
planet_name = "nothing"
planet_temp = -270
planet_size = 0.0
planet_life = 0.0

root = Tk()
#root.state('zoomed')
Space(root)
root.mainloop()

enter image description here

enter image description here

Upvotes: 2

Related Questions