Jak sprawić, by tkinter canvas dynamicznie zmieniał rozmiar na szerokość okna?
Muszę pobrać kanwę w tkinterze, aby ustawić jej szerokość na szerokość okna, a następnie dynamicznie zmienić rozmiar kanwy, gdy użytkownik uczyni okno mniejszym/większym.
Czy Jest jakiś sposób na zrobienie tego (łatwo)?
26
2 answers
Pomyślałem, że dodam jakiś dodatkowy kod, aby rozwinąć @fredtantini ' s answer, ponieważ nie zajmuje się tym, jak zaktualizować kształt widżetów rysowanych na Canvas
.
Aby to zrobić, musisz użyć metody scale
i oznaczyć wszystkie widżety. Pełny przykład znajduje się poniżej.
from Tkinter import *
# a subclass of Canvas for dealing with resizing of windows
class ResizingCanvas(Canvas):
def __init__(self,parent,**kwargs):
Canvas.__init__(self,parent,**kwargs)
self.bind("<Configure>", self.on_resize)
self.height = self.winfo_reqheight()
self.width = self.winfo_reqwidth()
def on_resize(self,event):
# determine the ratio of old width/height to new width/height
wscale = float(event.width)/self.width
hscale = float(event.height)/self.height
self.width = event.width
self.height = event.height
# resize the canvas
self.config(width=self.width, height=self.height)
# rescale all the objects tagged with the "all" tag
self.scale("all",0,0,wscale,hscale)
def main():
root = Tk()
myframe = Frame(root)
myframe.pack(fill=BOTH, expand=YES)
mycanvas = ResizingCanvas(myframe,width=850, height=400, bg="red", highlightthickness=0)
mycanvas.pack(fill=BOTH, expand=YES)
# add some widgets to the canvas
mycanvas.create_line(0, 0, 200, 100)
mycanvas.create_line(0, 100, 200, 0, fill="red", dash=(4, 4))
mycanvas.create_rectangle(50, 25, 150, 75, fill="blue")
# tag all of the drawn widgets
mycanvas.addtag_all("all")
root.mainloop()
if __name__ == "__main__":
main()
26
Author: ebarr,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-09 21:28:35
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-09 21:28:35
Możesz użyć menedżera geometrii .pack
:
self.c=Canvas(…)
self.c.pack(fill="both", expand=True)
Powinno wystarczyć.
Jeśli płótno znajduje się wewnątrz ramki, zrób to samo dla ramki:
self.r = root
self.f = Frame(self.r)
self.f.pack(fill="both", expand=True)
self.c = Canvas(…)
self.c.pack(fill="both", expand=True)
Zobacz Effbot aby uzyskać więcej informacji.
Edit: jeśli nie chcesz "pełnowymiarowego" płótna, możesz powiązać płótno z funkcją:
self.c.bind('<Configure>', self.resize)
def resize(self, event):
w,h = event.width-100, event.height-100
self.c.config(width=w, height=h)
Zobacz effbot ponownie dla zdarzeń i wiązań
8
Author: fredtantini,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-24 11:09:15
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-24 11:09:15