canvas命令在函数内部不起作用

时间:2017-12-20 21:19:35

标签: python python-3.x canvas tkinter

帮助!我使用的是python 3.5.2,函数self.new_game无效。它应该在画布上放置文字,但它什么都不做! 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()

1 个答案:

答案 0 :(得分:2)

我删除了frame1并将Canvas放入root,然后使用canvas.pack()在窗口中查看画布。
(但我可以使用self代替root并使用self.pack(),因为Space继承自Frame。它会更合乎逻辑)

之后我只能改变文字位置,因为窗口对我的屏幕来说太大了。

我使用变量CENTER_XCENTER_Y将文字置于中心,无论屏幕大小如何。

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