简体   繁体   English

_tkinter.TclError: 错误的屏幕距离“.!startpage”

[英]_tkinter.TclError: bad screen distance ".!startpage"

from pathlib import Path
import tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self._frame = None
        self.switch_frame(StartPage)

    def switch_frame(self, frame_class):
        """Destroys current frame and replaces it with a new one."""
        new_frame = frame_class(self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.place()

class StartPage(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)

        OUTPUT_PATH = Path(__file__).parent
        ASSETS_PATH = OUTPUT_PATH / Path("MENUASSETS")

        def relative_to_assets(path: str, ) -> Path:
            return ASSETS_PATH / Path(path)

        canvas = tk.Canvas(self,bg="#F7F7F7",height=470,width=600,bd=0,highlightthickness=0,relief="ridge")
        canvas.place(x=0, y=0)
        canvas.create_rectangle(0.0,0.0,396.0,470.0,fill="#000000",outline="")

        self.button_image_1 = tk.PhotoImage(file=relative_to_assets("button_admin.png"))
        button_1 = tk.Button(self,image=self.button_image_1,borderwidth=0,highlightthickness=0,command=lambda: master.show_frame(userinterface),relief="flat")
        button_1.place(x=70.0,y=66.0,width=256.0,height=74.0)

        self.button_image_2 = tk.PhotoImage(file=relative_to_assets("button_user.png"))
        button_2 = tk.Button(self,image=self.button_image_2,borderwidth=0,highlightthickness=0,command=lambda: print("button_2 clicked"),   relief="flat")
        button_2.place(x=75.0,y=198.0,width=256.0,height=74.0)

        self.button_image_3 = tk.PhotoImage(file=relative_to_assets("button_vc.png"))
        button_3 = tk.Button(self,image=self.button_image_3,borderwidth=0,highlightthickness=0,command=lambda: print("button_3 clicked"),relief="flat")
        button_3.place(x=70.0,y=330.0,width=256.0,height=74.0)

        self.button_image_4 = tk.PhotoImage(file=relative_to_assets("button_mexit.png"))
        button_4 = tk.Button(self,image=self.button_image_4,borderwidth=0,highlightthickness=0,command=lambda: print("button_4 clicked"),relief="flat")
        button_4.place(x=462.0,y=415.0,width=77.0,height=36.0)

        canvas.create_text(self,13.0,424.0,anchor="nw",text="MAYA",fill="#000000",font=("Sora Regular", 27 * -1))

        self.image_image_1 = tk.PhotoImage(file=relative_to_assets("logo.png"))
        self.image1 = canvas.create_image(self,500.0,145.0,image=self.image_image_1)

Error:错误:

_tkinter.TclError: bad screen distance ".!startpage"

You has wrongly passed self to the two lines:您错误地将self传递给了两行:

canvas.create_text(self,13.0,424.0,anchor="nw",text="MAYA",fill="#000000",font=("Sora Regular", 27 * -1))
...
self.image1 = canvas.create_image(self,500.0,145.0,image=self.image_image_1)

Remove self from them:从他们中删除self

canvas.create_text(13.0,424.0,anchor="nw",text="MAYA",fill="#000000",font=("Sora Regular", 27 * -1))
...
self.image1 = canvas.create_image(500.0,145.0,image=self.image_image_1)

Since place() is used to lay out widgets inside StartPage , it makes StartPage has size of 1x1.由于place()用于在StartPage内布置小部件,因此它使StartPage大小为 1x1。

You can specify the initial size of the root window and tell StartPage to fill the window as below:您可以指定根窗口的初始大小并告诉StartPage填充窗口,如下所示:

class SampleApp(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry('600x480') # specify initial window size
        ...

    def switch_frame(self, frame_class):
        ...
        self._frame.place(x=0, y=0, relwidth=1, relheight=1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM