Fenstergröße abfragen

Fragen zu Tkinter.
Antworten
PNS-Richi
User
Beiträge: 68
Registriert: Donnerstag 17. Januar 2008, 01:48

Irgendwie komme ich nicht weiter. Ich möchte die aktuelle Fenstergröße auslesen, ohne diese zuvor gesetzt zu haben. Also das Fenster passt sich den Widgets an.

Irgendwie ne doofe Frage, aber in Tk habe ich noch keine möglichkeit gefunden sie aus zu lesen.

Wäre toll, wenn mir wer auf die sprünge helfen könnte :)

Was mich auch noch interessiert, wie kann ein Widget 100% in einem Grid einnehmen?

lg Richi
Ene Uran
User
Beiträge: 125
Registriert: Sonntag 17. September 2006, 20:14
Wohnort: Hollywood

Das Beispiel wird hoffentlich einiges antworten:

Code: Alles auswählen

# looking at the Tkinter grid() layout manager
#
# The size of a grid cell in a given row or column is
# determined by its largest widget.
#
# If you give a grid layout more space than it needs,
# it will fill this space from its center on out.
# A way around this is to use place() with a frame and
# then grid() within this frame.
#
# Grid does not have an anchor positioner.
#
# You can use place() and grid(), but not pack() and grid()
# within the same container widget.
#
# You can put a child grid into a parent grid.

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

root = tk.Tk()
root.title('Grid layout')
root['bg'] = 'yellow'

b1 = tk.Button(root, text="Button1")
b2 = tk.Button(root, text="Button2", bg='green')
b3 = tk.Button(root, text="Button3")
b4 = tk.Button(root, text="Button4")
b5 = tk.Button(root, text="Button5")
b6 = tk.Button(root, text="Button6")
b7 = tk.Button(root, text="Button7", bg='blue')
# button b8 has a 3 row text
b8 = tk.Button(root, text="\nButton8\n", bg='red')

b1.grid(row=0, column=1)
# use sticky=tk.E+tk.W or sticky='ew'
b2.grid(row=1, column=0, columnspan=3, sticky='ew')
b3.grid(row=2, column=0, pady=5)
b4.grid(row=2, column=1)
# gives the entire column=2 padx=10 padding!
b5.grid(row=2, column=2, padx=10)
b6.grid(row=3, column=2)
# forces button b7 to take up padded space too
b7.grid(row=4, column=2, ipadx=10)
# button b8 has a 3 row text, so give it rowspan=3
b8.grid(row=5, column=0, rowspan=3, columnspan=3, sticky='ew')

# dictionary of grid info for button b8
print( b8.grid_info() )
# specific info about b8
print( b8.grid_info()['row'] )  # 5

# get size of root, update is needed!
root.update()
width = root.winfo_width()  # 158
print(width)
height = root.winfo_height()  # 174
print(height)

root.mainloop()
Atomkraftwerkaktienbesitzer
Benutzeravatar
wuf
User
Beiträge: 1529
Registriert: Sonntag 8. Juni 2003, 09:50

Hallo PNS-Richi

Hier noch eine Möglichkeit die Fenstergrösse ohne fenster.update() abzufragen:

Code: Alles auswählen

import Tkinter as tk
app_win = tk.Tk()

app_win_width = app_win.winfo_reqwidth()
app_win_height = app_win.winfo_reqheight()

print app_win_width, app_win_height

app_win.mainloop()
Das Hauptfenster wird beim Start von Tk scheinbar automatisch mit width=200 und height=200 initialisiert.

Gruss wuf :wink:
Take it easy Mates!
pyStyler
User
Beiträge: 311
Registriert: Montag 12. Juni 2006, 14:24

oder so :wink:

Code: Alles auswählen

import Tkinter 

root = Tkinter.Tk()

def window_size():

    print root.winfo_width(), 'WIDTH', root.winfo_height(), 'HEIGHT'

btn = Tkinter.Button(text='window size', command=window_size)
btn.pack()

root.mainloop()
Benutzeravatar
wuf
User
Beiträge: 1529
Registriert: Sonntag 8. Juni 2003, 09:50

Hallo pyStyler

Das Fenster bekommt mit dem einbetten eines Tk-Widget in deinem Fall (Tk-Button) scheinbar automatisch ein fenster.update() und das Fenster übernimmt die Abmessungen des Button-Widgets. Auf folgende Art ist dies noch nicht der Fall:

Code: Alles auswählen

import Tkinter

root = Tkinter.Tk()

def window_size():

    print root.winfo_width(), 'WIDTH', root.winfo_height(), 'HEIGHT'

window_size()

root.mainloop()

Code: Alles auswählen

1 WIDTH 1 HEIGHT
Danke für deinen Beitrag.

Gruss wuf :wink:
Take it easy Mates!
Benutzeravatar
wuf
User
Beiträge: 1529
Registriert: Sonntag 8. Juni 2003, 09:50

.......und so werden die ursprünglich initialisierten Fensterabmessungen beibehalten (nur unter Linux getestet):

Code: Alles auswählen

import Tkinter

def window_size():

    print root.winfo_width(), 'WIDTH', root.winfo_height(), 'HEIGHT'

root = Tkinter.Tk()

root_width = root.winfo_reqwidth()
root_height = root.winfo_reqheight()

root.geometry('%dx%d' % (root_width, root_height))

btn = Tkinter.Button(text='window size', command=window_size)
btn.pack()
root.mainloop()

Gruss wuf :wink:
Take it easy Mates!
Antworten