Python Tkinter Canvas最初或在调整大小时填充Tk()窗口,但不会

时间:2017-06-26 22:04:31

标签: python canvas tkinter resize fullscreen

如果这是我的代码:

from tkinter import *
    class Program:
        def __init__(self):
            self.tk = Tk()
            self.tk.attributes("-topmost", 1)
            self.canvas = Canvas(self.tk, height=500, width=500, highlightthickness=0)
            self.canvas.pack(fill="both", expand=1)
            self.width = 500
            self.height = 500
            self.tk.bind("<Configure>", self.resize)
            self.input = Entry(self.tk)
            self.input.pack(fill="x", expand=1)
            self.button = Button(self.tk, text="Start!", command=self.pressed)
            self.button.pack(fill="x", expand=1)
            self.pressed = 0
            self.start = time()
        def mainloop(self):
            while 1:
                self.canvas.delete("all")
                self.canvas.create_rectangle(0, 0, self.width, self.height, fill="#4dffff", width=0)
                self.tk.update()
        def pressed(self):
            self.pressed = 1
        def resize(self, event):
            self.width = event.width
            self.height = event.height
Program().mainloop()

如果我运行它,则会打开此窗口: Initial Program

如果我移动窗口,它看起来像这样: enter image description here

一旦我调整它(例如通过全屏)我就会看到这个窗口: enter image description here

我如何更改代码以保持画布(我的意思是它的彩色部分)覆盖Tk()窗口的未覆盖部分的结果?

1 个答案:

答案 0 :(得分:0)

你有很多错误和/或不必要的代码导致各种各样的问题。归结为你不应该创建自己的static_assert(sizeof(productData<int>)==sizeof(productDataAsArray<int>::arr), "padding/alignment inconsistency"); static_assert(sizeof(productData<char>)==sizeof(productDataAsArray<char>::arr), "padding/alignment inconsistency"); static_assert(sizeof(productData<double>)==sizeof(productDataAsArray<double>::arr), "padding/alignment inconsistency"); static_assert(sizeof(productData<char*>)==sizeof(productDataAsArray<char*>::arr), "padding/alignment inconsistency"); class alignas(16) testClass { int x; int y; virtual void test() {}; }; static_assert(sizeof(productData<testClass>)==sizeof(productDataAsArray<testClass>::arr), "padding/alignment inconsistency"); 。使用task build \ { } 提供的那个,除非您深刻理解为什么会这样。

在尝试执行任何其他操作之前,请先使用此代码:

mainloop