0

If this is my code:

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()

If I run it then this window opens: Initial Program

If I then move the window it looks like this: enter image description here

As soon as I resize it (by fullscreen for instance) I get this window: enter image description here

How would I have to change the code to consistentely have the result where the canvas (I mean the colored part of it) covers the uncovered part of the Tk() window?

Ron Lauterbach
  • 107
  • 1
  • 1
  • 12

1 Answers1

0

You have a lot of wrong and/or unnecessary code that is causing all sorts of problems. It boils down to the fact that you shouldn't be creating your own mainloop. Use the one that Tkinter provides unless you deeply understand why that is the case.

Start with this code before trying to do anything else:

from tkinter import *
from time import time

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.input = Entry(self.tk)
        self.button = Button(self.tk, text="Start!", command=self.pressed)

        self.button.pack(side="bottom", fill="x", expand=0)
        self.input.pack(side="bottom", fill="x", expand=0)
        self.canvas.pack(fill="both", expand=1)

        self.pressed = 0
        self.start = time()

    def begin(self):
        self.tk.mainloop()

    def pressed(self):
        self.pressed = 1

Program().begin()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Do I add further code in the begin method or outside the class in this case and do you have to repeatedly (loop) type self.tk.mainloop()? – Ron Lauterbach Jun 26 '17 at 22:55
  • @RonLauterbach: that all depends on what "further code" you're talking about. In general I would say no, there doesn't need to be any more code in `begin`. No, you do not "have to repeatedly (loop) type self.tk.mainloop()`. Like it's name implies, it _is_ a loop already. You should call `mainloop` exactly once. – Bryan Oakley Jun 26 '17 at 23:16