Feste Treeview-Größe

Fragen zu Tkinter.
Antworten
DMD-OS
User
Beiträge: 20
Registriert: Freitag 28. Dezember 2018, 13:52

Mittwoch 30. Januar 2019, 13:54

hi
ich habe zwei treeviews, die jeweils in einem einzelframe sind. beide treeview-frames sind in einem gesamt-frame enthalten.

treeview 1 = mastertree(frame, ...)
treeview 2 = slavetree(frame, ...)

mastertree ist immer gesetzt. durch umschalten kann man nun entweder den einen slavetree oder einen anderen schalten.
da die beiden slavetrees unterschiedlich viele columns besitzen, wird dementsprechend die breite des slavetree-frames verändert.
ich versuche nun, das zu verhindert, indem ich beiden frames (self.frame_full und self.frame_fine) versuche mitzuteilen, daß sie unverändert mittig nebeneinander sitzen sollen.
leider ohne erfolg :cry:

code snippet:

Code: Alles auswählen

# GESAMT-Frame
self.frame_all = tk.Frame(self.middle, bg="yellow") # <----------- gesamtframe für beide
self.frame_all.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)   

# MASTERTREE-Frame
frame_full = tk.Frame(self.frame_all, bg="blue")  #<-----------  master-einzelframe (immer gesetzt)
frame_full.pack(side=self.frames_var.get(), fill=tk.BOTH, expand=True, padx=10, pady=10)

# SLAVETREE-Frame (entweder für slavetree 1 oder 2)
frame_fine = tk.Frame(self.frame_all, bg="green")  #<-----------  slave-einzelfenster
frame_fine.pack(side=self.frames_var.get(), fill=tk.BOTH, expand=True, padx=10, pady=10)

style_master = ttk.Style()
style_master.configure("firststyle.Treeview.Heading", font=('Arial', 9, 'bold', 'underline'))
style_master.layout("firststyle.Treeview", [('firststyle.Treeview.treearea', {'sticky': 'nswe'})])
self.mastertree = ttk.Treeview(frame_full, style="firststyle.Treeview")
self.mastertree.heading('#0', text='GESAMTANSICHT:', anchor=tk.W)
self.mastertree.pack(side=self.frames_var.get(), fill=tk.BOTH, expand=True, padx=5, pady=5)
self.mastertree.bind('<ButtonRelease-1>', self.select_item_FIRST)

if self.tables_var.get() == 0:
            lb_fine = tk.Label(frame_fine, text="Teilansicht - Knotenstruktur", font=('Arial', 11, 'bold'), bg="darkgrey", relief=tk.FLAT, anchor=tk.W)
            lb_fine.pack(side=tk.TOP, fill=tk.X)
            style_slave = ttk.Style()
            style_slave.configure("secondstyle.Treeview.Heading", font=('Arial', 11, 'bold', 'underline'))
            style_slave.layout("secondstyle.Treeview", [('secondstyle.Treeview.treearea', {'sticky': 'nswe'})])
            self.slavetree = ttk.Treeview(frame_fine, style="secondstyle.Treeview")
            # self.slavetree.bind('<ButtonRelease-1>', self.select_item_LAST)
            self.slavetree.pack(side=self.frames_var.get(), fill=tk.BOTH, expand=True, padx=5, pady=5)
else:
            lb_fine = tk.Label(frame_fine, text="Teilansicht - Tabellenstruktur 1", font=('Arial', 11, 'bold'), bg="darkgrey", relief=tk.FLAT, anchor=tk.W)
            lb_fine.pack(side=tk.TOP, fill=tk.X)
            style_slave = ttk.Style()
            style_slave.configure("secondstyle.Treeview.Heading", font=('Arial', 9, 'bold', 'underline'))
            style_slave.layout("secondstyle.Treeview", [('secondstyle.Treeview.treearea', {'sticky': 'nswe'})])
            self.header_fine = ['Strukturebene 1', 'Strukturebene 2', 'Strukturebene 3', 'Strukturebene 4', 'Strukturebene 5', 'Strukturebene 6']
            self.slavetree = ttk.Treeview(frame_fine, columns=self.header_fine, show="headings", style="secondstyle.Treeview")
            # self.slavetree.bind('<ButtonRelease-1>', self.select_item_LAST)
            self.slavetree.pack(side=self.frames_var.get(), fill=tk.BOTH, expand=True, padx=5, pady=5)
            for col in self.header_fine:
                self.slavetree.heading(col, text=col.title())
                self.slavetree.column(col, width=tkFont.nametofont("TkDefaultFont").metrics()['linespace']*2+10)

hilfe...
Benutzeravatar
__blackjack__
User
Beiträge: 2270
Registriert: Samstag 2. Juni 2018, 10:21

Freitag 1. Februar 2019, 05:22

@DMD-OS: Also mir wird aus der Beschreibung nicht klar was das Problem ist. Kannst Du ein lauffähiges Codebeispiel zeigen welches das Problem demonstriert? Wäre ein `ttk.Notebook` eventuell eine Lösung für das Problem?
»I'm not going to ride on a magic carpet!« he hissed.
»I'm afraid of grounds!«
»You mean heights,« said Conina. »And stop being silly.«
»I know what I mean! It's the grounds that kill you!« — Terry Pratchett, Sourcery
Benutzeravatar
wuf
User
Beiträge: 1501
Registriert: Sonntag 8. Juni 2003, 09:50

Freitag 1. Februar 2019, 06:48

Hi DMD-OS

So wie ich dich verstehe möchtest du, dass die Fenstergrösse des Slave Tree's bei einem anderen Datensatz Header gleich gross bleibt wid die Fenstergrösse des Master Treefensters. Hier eine mögliche Lösungsvariante. Für die Simulation stehen vier Datensatz Headers verschiedener Grösse zur Verfügung (self.tables). Mit einem linken Maustastenklick ins Master Treefenster wechselt der Datensatz Header im Slave Treefenster. Das Slave Treefenster ist mit Scrollbars versehen um durch grösser zu navigieren.

Code: Alles auswählen

import tkinter as tk
from tkinter import ttk
import tkinter.font as tk_font

APP_TITLE = "Feste Treefenstergrösse"

APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 400
APP_HEIGHT = 300


class Application(object):

    def __init__(self, main_win):
        self.main_win = main_win
        
        self.build()
        
    def build(self):
        self.main_frame = tk.Frame(self.main_win, bg="yellow")
        self.main_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)

        # MASTERTREE-Frame
        frame_master_tree = tk.Frame(self.main_frame, bg="blue")
        frame_master_tree.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=10,
            pady=10)

        style_master = ttk.Style()
        style_master.configure("firststyle.Treeview.Heading",
            font=('Arial', 9, 'bold', 'underline'))
        style_master.layout("firststyle.Treeview",
            [('firststyle.Treeview.treearea', {'sticky': 'nswe'})])
        
        self.master_tree = ttk.Treeview(frame_master_tree,
            style="firststyle.Treeview")
        self.master_tree.heading('#0', text='GESAMTANSICHT:', anchor=tk.W)
        self.master_tree.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
        self.master_tree.bind(
            '<ButtonRelease-1>', self.master_tree_button_event)

        # SLAVETREE-Frame
        frame_slavetree = tk.Frame(self.main_frame, bg="green")
        frame_slavetree.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=10,
            pady=10)
        
        border_frame = tk.Frame(frame_slavetree, bg="green")
        border_frame.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
        
        label_slavetree = tk.Label(border_frame, text="Teilansicht - Knotenstruktur",
            font=('Arial', 11, 'bold'), bg="darkgrey", relief=tk.FLAT,
            anchor=tk.W)
        label_slavetree.pack(fill=tk.X)
        
        frame_canvas_slavetree = tk.Frame(border_frame)
        frame_canvas_slavetree.pack(fill=tk.BOTH, expand=True, )
        
        self.canvas = tk.Canvas(frame_canvas_slavetree, highlightthickness=0,
            bg="steelblue", width=1, height=1)
        self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
        self.canvas.bind("<Configure>", self.canvas_configure_event)

        canvas_interior_frame = tk.Frame(self.canvas)      
        
        style_slave = ttk.Style()
        style_slave.configure("secondstyle.Treeview.Heading",
            font=('Arial', 11,'bold', 'underline'))
        style_slave.layout("secondstyle.Treeview",
            [('secondstyle.Treeview.treearea', {'sticky': 'nswe'})])
        
        self.slave_tree = ttk.Treeview(canvas_interior_frame, 
            style="secondstyle.Treeview", show="headings")
        self.slave_tree.pack(side=tk.TOP, fill=tk.BOTH)
        self.slave_tree.bind("<Configure>", self.slavetree_configure_event)
        
        self.canvas.create_window(
            0, 0, anchor='nw', window=canvas_interior_frame)
        
        yscrollbar = tk.Scrollbar(frame_canvas_slavetree, orient="vertical",
            command=self.canvas.yview)
        yscrollbar.pack(side=tk.LEFT, fill=tk.Y)

        xscrollbar = tk.Scrollbar(border_frame, orient="horizontal",
            command=self.canvas.xview)
        xscrollbar.pack(fill=tk.X)
        
        self.canvas.configure(
            yscrollcommand=yscrollbar.set,
            xscrollcommand=xscrollbar.set)
        
        self.table_pointer = 0
        self.tables = [
            ['Strukturebene 1', 'Strukturebene 2'],
            ['Strukturebene 1', 'Strukturebene 2', 'Strukturebene 3'],
            ['Strukturebene 1', 'Strukturebene 2', 'Strukturebene 3',
                'Strukturebene 4', 'Strukturebene 5'],
            ['Strukturebene 1', 'Strukturebene 2', 'Strukturebene 3',
                'Strukturebene 4', 'Strukturebene 5', 'Strukturebene 6']
            ]
        
        self.select_table()
         
    def slavetree_configure_event(self, event):
        self.canvas_configure_event()
                      
    def canvas_configure_event(self, event=None):
        self.slave_tree.configure(height=self.canvas.winfo_height())
        
        slave_tree_width = self.slave_tree.winfo_width()
        slave_tree_height = self.slave_tree.winfo_height()
        
        self.canvas.configure(scrollregion=(
            0, 0, slave_tree_width, slave_tree_height))
    
    def master_tree_button_event(self, event):
        self.select_table()
                  
    def select_table(self):
        self.header_fine = self.tables[self.table_pointer]
        self.slave_tree.configure(columns=self.header_fine)
        print("select_Table", self.table_pointer, self.header_fine)
        
        for col in self.header_fine:
            self.slave_tree.heading(col, text=col.title())

        if self.table_pointer == len(self.tables)-1:
            self.table_pointer = 0
        else:
            self.table_pointer += 1
        
           
def main():
    main_win = tk.Tk()
    main_win.title(APP_TITLE)
    #main_win.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))
    #main_win.geometry("{}x{}".format(APP_WIDTH, APP_HEIGHT))
    app = Application(main_win)
    main_win.mainloop()
 
 
if __name__ == '__main__':
    main()      
Gruss wuf :-)
Take it easy Mates!
DMD-OS
User
Beiträge: 20
Registriert: Freitag 28. Dezember 2018, 13:52

Freitag 1. Februar 2019, 08:54

genau das meinte ich. eigentlich viel mehr als ich wollte. :shock:
ich glaub, bei mir ist noch das hauptproblem ordnung da rein zu gekommen. das ist
echt nicht so leicht, eine saubere struktur aufzubauen.
HAB DANK :lol:
DMD-OS
User
Beiträge: 20
Registriert: Freitag 28. Dezember 2018, 13:52

Samstag 9. Februar 2019, 15:23

hey
ich habe leider noch ein zusatzproblem.
ich möchte es natürlich auch noch gern erreichen, daß die beiden frames mit einer überschrift befüllt werden, die ebenfalls mittig nebeneinander stehen, egal wie lang
der textstring ist.

Code: Alles auswählen

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import time
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
from Datenbank import Database


class StartPage(Database):

    def __init__(self, window, path_db_file):
        Database.__init__(self, path_db_file)
        self.path_db_file = path_db_file
        self.db_basetable = Database(self.path_db_file)

        self.window = window
        self.window.state('zoomed')
        self.window.overrideredirect(True)
        self.window.resizable(False, False)
        self.window.columnconfigure(0, weight=2)
        self.window.iconbitmap(default='minlogo_piepenbrock.ico')
        self.window.bind('<Map>', self.check_map)
        self.window.bind('<Unmap>', self.check_map)

        ############
        # HEADER
        ############
        self.header = tk.Frame(self.window, height=45, bg="grey")
        self.header.pack_propagate(False)
        self.header.pack(side=tk.TOP, fill=tk.X)
        self.header.bind('<B1-Motion>', self.move_window)

        ###########
        # MENUBAR
        ###########
        self.menubar = tk.Frame(self.window, bg='darkgrey', relief=tk.GROOVE, bd=2)
        self.menubar.pack(side=tk.TOP, fill=tk.X)

        ###########
        # MIDDLE
        ###########
        self.middle = tk.Frame(self.window, bg="red")
        self.middle.pack(expand=True, fill=tk.BOTH)

        ###########
        # FOOTER
        ###########
        self.footer = tk.Frame(self.window, height=50, bg='grey')
        self.footer.pack_propagate(False)
        self.footer.pack(side=tk.BOTTOM, fill=tk.X)

        ####################################################
        # ---> MENUBAR

        # LABEL: SETTINGS - MENUBAR
        self.lb_puffer = tk.Label(self.menubar, bg='darkgrey', relief=tk.FLAT)
        self.lb_puffer.pack(side=tk.LEFT, padx=10)

        img_node = Image.open('node_switch.png').resize((27, 24))
        image_node = ImageTk.PhotoImage(img_node)
        self.lb_node = tk.Button(self.menubar, image=image_node)
        self.lb_node.image = image_node
        self.lb_node.pack(side=tk.LEFT, padx=2, pady=4)
        self.lb_node.bind("<Button-1>", self.select_SLAVETABLES)

        ####################################################
        ####################################################
        # ---> MIDDLE

        # VALUES: TABLE FRAMES - MIDDLE
        self.frames_var = tk.StringVar()
        self.frames_var.set('left')

        self.tables_var = tk.IntVar()
        self.tables_var.set(1)

        # FRAME: TREEVIEW-BOTH - MIDDLE
        self.frame_all = tk.Frame(self.middle, bg="yellow")
        self.frame_all.pack(fill=tk.BOTH, expand=True, padx=20, pady=20)

        # FRAME: TABLE 1 - MIDDLE
        self.frame_full = tk.Frame(self.frame_all, bg="blue")
        self.frame_full.pack(side=self.frames_var.get(), fill=tk.BOTH, expand=True, padx=10, pady=10)

        # FRAME: TABLE 2 - MIDDLE
        self.frame_fine = tk.Frame(self.frame_all, bg="green")
        self.frame_fine.pack(side=self.frames_var.get(), fill=tk.BOTH, expand=True, padx=10, pady=10)

        ####################################################
        # ---> VALUES
        self.table_pointer = 0

        ####################################################
        # ---> START
        self.start_MASTERTREE()
        self.start_SLAVETREE()
        self.start_FINISHBUTTONS()


    def start_MASTERTREE(self):
        # FRAME: MASTERTREE - MIDDLE
        border_master = tk.Frame(self.frame_full, bg="pink")
        border_master.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)

        label_mastertree = tk.Label(border_master, text="Gesamtansicht - Knotenstruktur", font=('Arial', 9, 'bold'), bg="darkgrey", relief=tk.FLAT, anchor=tk.W)
        label_mastertree.pack(fill=tk.X)



    def start_SLAVETREE(self):
        # FRAME: SLAVETREE - MIDDLE
        border_slave = tk.Frame(self.frame_fine, bg="pink")
        border_slave.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
        self.tables_var = tk.StringVar()
        label_slavetree = tk.Label(border_slave, textvariable=self.tables_var, font=('Arial', 9, 'bold'), bg="darkgrey", relief=tk.FLAT, anchor=tk.W)
        label_slavetree.pack(fill=tk.X)
        self.tables_var.set("Feinansicht - Knotenstruktur")



    def start_FINISHBUTTONS(self):
        # FINISH BUTTONS - MIDDLE
        finish_buttons = tk.Frame(self.middle, bg="pink")
        finish_buttons.pack(side=tk.BOTTOM, fill=tk.BOTH, padx=20, pady=5)
        tk.Button(finish_buttons, text='Close', width=15, command=self.window.quit).pack(pady=5)

        ####################################################
        # ---> FOOTER

        # FRAME: IMAGE - FOOTER
        edge_frame = tk.Frame(self.footer, bg='lightgrey', bd=1)
        edge_frame.pack(side=tk.LEFT, padx=10)


    def select_SLAVETABLES(self, _):
        table_names = ["Feinansicht - Knotenstruktur", "Tabellenansicht 1 - Feinstruktur", "Tabellenansicht 2 - Feinstruktur"]
        if self.table_pointer == len(table_names) - 1:
            self.table_pointer = 0
        else:
            self.table_pointer += 1
        self.tables_var.set(table_names[self.table_pointer])



    def move_window(self, event):
        self.window.geometry('+{0}+{1}'.format(event.x_root, event.y_root))

    def check_map(self, event):
        if str(event) == "<Map event>":
            self.window.overrideredirect(1)
            self.window.state('zoomed')

    def exitProgram(self, _):
        self.window.quit()


def main():
    db_name = "DB_AddFM.db"
    pathfile_db_folder = os.path.join(os.path.expanduser("~"), "Desktop", "Datenbanksystem")
    pathfile_db = os.path.join(pathfile_db_folder, db_name)

    root = tk.Tk()
    StartPage(root, pathfile_db)
    root.mainloop()


if __name__ == '__main__':
    main()
DMD-OS
User
Beiträge: 20
Registriert: Freitag 28. Dezember 2018, 13:52

Sonntag 17. Februar 2019, 15:34

Habe die Lösung gefunden:

Code: Alles auswählen

frame_1 = tk.Frame(.....)
frame_1.propagate(False)
frame_1.pack(...)
Antworten