ich habe eine GUI mit tkinter erstellt und eine X_Scrollbar ist eingerichtet.
Bei überlangen Texten, lässt sich die Scrollbar zwar bewegen, aber der Fensterinhalt bewegt sich nicht.
Nachfolgender Code ist lauffähig und sollte zum Testen funktionieren.
Vielleicht fällt Euch der Fehler auf, dass zwar die Scrollbar sich bewegt aber das Fenster nicht!
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# For Python3.x
import tkinter as tk
"""Programmname"""
PROGRAMMNAME = 'gui_Dataset_Vertical.py'
class DatasetVertical(object):
"""
Tabellarische Ausgabe der Daten im vertikalen Datenformat.
"""
def __init__(self, tk_status, data, labels, color):
tk_check = {'tk' : tk.Tk, 'top' : tk.Toplevel}
self.my_win = tk_check[tk_status]()
self.my_win.title('DATASET VERTICAL')
# Ermittle die Größe des Bildschirmes
self.screenx = self.my_win.winfo_screenwidth()
self.screeny = self.my_win.winfo_screenheight()
self.label_names = labels
self.name_width = round(int(max([len(name) for name in labels])) * 1.3)
# Erstellung Dictionary für GUI-Ausgabe.
self._dict_ = dict()
max_item = set()
for i, row in enumerate(data):
for c, x in enumerate(row):
try:
self._dict_[i].update({c : x})
except KeyError:
self._dict_[i] = {c : x}
max_item.add(len(x))
self.max_width = round(max(max_item) + 3)
self.color = color
if self.color == '':
self.color = 'grey'
self.ipadx = 5 # Abstand zu Text in x
self.ipady = 2 # Abstand zu Text in y
self.window = tk.Frame(self.my_win, bg=self.color)
self.window.grid(row=0, column=0, sticky=tk.NSEW)
self.window.grid_rowconfigure(0, weight=1)
self.window.grid_columnconfigure(0, weight=1)
def start(self):
# Buttons erstellen
self.button_frame = tk.Label(self.window)
self.button_frame.grid(row=1, column=0)
button_text = 'Schließen'
self.len_max = len(button_text)
tk.Button(self.button_frame, width=self.len_max, text=button_text,
command=self.close).grid(row=0, column=0)
self.canvas = tk.Canvas(self.window, bd=0)
self.canvas.grid(row=0, column=0, sticky=tk.NSEW)
self.canvas1 = tk.Canvas(self.canvas, bg='darkgrey', bd=0)
self.canvas1.grid(row=0, column=0, sticky=tk.NSEW)
## scrollbar erstellen
yscrollbar = tk.Scrollbar(self.canvas, orient=tk.VERTICAL,
bg = 'darkgrey', activebackground = 'lightsteelblue')
yscrollbar.grid(row=0, column=2, sticky=tk.NS)
xscrollbar = tk.Scrollbar(self.canvas, orient=tk.HORIZONTAL,
bg = 'darkgrey', activebackground = 'lightsteelblue')
xscrollbar.grid(row=1, column=1, sticky=tk.EW)
self.canvas2 = tk.Canvas(self.canvas, bg='darkgrey', bd=0,
scrollregion=(0, 0, 0, 0),
xscrollcommand=xscrollbar.set,
yscrollcommand=yscrollbar.set)
self.canvas2.grid(row=0, column=1, sticky=tk.NSEW)
self.build_table()
yscrollbar.config(command=self.canvas2.yview)
xscrollbar.config(command=self.canvas2.xview)
self.canvas.update_idletasks()
x, y, w, h = self.canvas.bbox(tk.ALL)
self.xpos += 20 # Y-Scrollbar
self.ypos += 40 # Button-Spalte
new_w = w
if self.xpos > self.screenx:
new_w = self.screenx
w = self.xpos + x
self.canvas2.config(scrollregion=(x, y, w, h),
width=new_w, height=self.ypos)
def build_table(self):
"""Tabellenspalte erstellen"""
width = self.max_width
if ('BENENNUNG' in self.label_names
or 'BESCHREIBUNG' in self.label_names):
width=50
self.ypos = 0
for i, name in enumerate(self.label_names):
self.xpos = 0
height = 1
if name == 'BENENNUNG':
height = 2
elif name == 'BESCHREIBUNG':
height = 5
label = tk.Label(self.canvas1, width=self.name_width, height=height,
text=name, bd=1, highlightthickness=1, anchor=tk.W)
label.grid(row=i, column=0, padx=self.ipadx, pady=self.ipady)
self.xpos += label.winfo_reqwidth()
self.canvas.create_window(self.xpos, self.ypos)
for i2column in range(len(self._dict_)):
text = self._dict_[i2column][i]
label2 = tk.Label(self.canvas2, width=width, height=height,
wraplength=395, justify=tk.LEFT, text=text, bd=1,
highlightthickness=1, anchor=tk.W)
label2.grid(row=i, column=i2column, padx=self.ipadx,
pady=self.ipady)
self.xpos += label2.winfo_reqwidth()
self.ypos += label2.winfo_reqheight()
def close(self):
self.my_win.quit()
self.my_win.destroy()
def run(self):
self.start()
# Setze Fenster mittig.
hdw = (self.screenx - self.xpos) / 2
if self.xpos > self.screenx:
hdw = self.screenx
hdh = (self.screeny - self.ypos) / 2
if self.ypos > self.screeny:
hdh = self.screeny
self.my_win.geometry('{}x{}+{}+{}'.format(self.xpos, self.ypos,
round(hdw), round(hdh)))
self.my_win.mainloop()
def main():
tk_status = 'tk'
products = [('048', '80108095', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'),
('048', '80108095', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'),
('048', '80108095', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'),
('048', '80108095', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'),
('048', '80108095', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb')]
labels = ['ID', 'NUMBER', 'BENENNUNG', 'BESCHREIBUNG']
color = 'red'
DatasetVertical(tk_status, products, labels, color).run()
if __name__ == '__main__':
main()
Grüße Nobuddy