【问题标题】:_tkinter.TclError: bad screen distance ".!startpage"_tkinter.TclError:屏幕距离错误“.!startpage”
【发布时间】:2021-11-02 09:00:10
【问题描述】:
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)

错误:

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

【问题讨论】:

  • 提供minimal reproducible example和完整的错误回溯(从单词“Traceback”开始),我也建议不要使用.place,使用.pack.grid(可以混用)在单独的容器中)
  • 谢谢!建议!
  • 请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example

标签: python tkinter


【解决方案1】:

您错误地将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)

从他们那里删除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)

由于place() 用于在StartPage 内布局小部件,因此StartPage 的大小为1x1。

您可以指定根窗口的初始大小并告诉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)

【讨论】:

  • 我删除了在线上的自我,但似乎没什么我想知道为什么
  • @zakwan 这是因为您使用place()StartPage 中布置小部件,导致StartPage 的大小为1x1。
  • @zakwan 答案已更新,建议显示StartPage
  • 哇!有用!非常感谢!
猜你喜欢
  • 2012-05-09
  • 1970-01-01
  • 2011-11-18
  • 1970-01-01
  • 1970-01-01
  • 2013-08-22
  • 1970-01-01
  • 1970-01-01
  • 2019-02-18
相关资源
最近更新 更多