Feste Framegröße mit Text

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

Dienstag 12. Februar 2019, 13:28

hi
ich versuche, trotz unterschiedlicher Textlängen (für mastertext und slavetext), beide Frames genau in der Mitte zu behalten.
Nur wenn beide Texte gleich lang sind, passt es :twisted: :evil:

Code: Alles auswählen

#!/usr/bin/env python3

import tkinter as tk

# Hier kann man den Text ändern... :)
master_text = "Guten Morgen, wie gehts?"
slave_text = "Hallo!"

class StartPage(tk.Frame):

    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.parent = parent
        self.parent.state('zoomed')
        self.parent.overrideredirect(True)
        self.parent.resizable(False, False)
        self.parent.bind('<Map>', self.check_map)
        self.parent.bind('<Unmap>', self.check_map)

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

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


    def start_MIDDLE(self):
        # FRAME: SIDEBAR - MIDDLE
        sidebar = tk.Frame(self.middle, width=28, bg="green")
        sidebar.pack(side=tk.LEFT, fill=tk.Y)

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

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

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

        # MATERTREE-Frame
        self.frame_mastertree = tk.Frame(frame_total, bg="pink")
        self.frame_mastertree.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=5, pady=5)
        self.start_MASTERTREE()

        # SLAVETREE-Frame
        self.frame_slavetree = tk.Frame(frame_fine, bg="pink")
        self.frame_slavetree.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=5, pady=5)
        self.start_SLAVETREE()

    def start_MASTERTREE(self):
        border_master_text = tk.Frame(self.frame_mastertree)
        border_master_text.pack(side=tk.TOP, fill=tk.X)
        lb_master_var = tk.StringVar()
        label_mastertree = tk.Label(border_master_text, textvariable=lb_master_var, font=('Arial', 9, 'bold'), bg="darkgrey", relief=tk.FLAT, anchor=tk.W)
        label_mastertree.pack(fill=tk.X)
        border_master_tree = tk.Frame(self.frame_mastertree, bg="yellow")
        border_master_tree.pack(side=tk.TOP, fill=tk.BOTH)
        lb_master_var.set(master_text)


    def start_SLAVETREE(self):
        border_slave_text = tk.Frame(self.frame_slavetree)
        border_slave_text.pack(side=tk.TOP, fill=tk.X)
        lb_slave_var = tk.StringVar()
        label_slavetree = tk.Label(border_slave_text, textvariable=lb_slave_var, font=('Arial', 9, 'bold'), bg="darkgrey", relief=tk.FLAT,anchor=tk.W)
        label_slavetree.pack(fill=tk.X)
        border_slave_tree = tk.Frame(self.frame_slavetree, bg="yellow")
        border_slave_tree.pack(side=tk.TOP, fill=tk.BOTH)
        lb_slave_var.set(slave_text)

    def start_FOOTER(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.parent.quit).pack(pady=5)

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

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

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


def main():
    root = tk.Tk()
    StartPage(root)
    root.mainloop()


if __name__ == '__main__':
    main()
Benutzeravatar
wuf
User
Beiträge: 1501
Registriert: Sonntag 8. Juni 2003, 09:50

Mittwoch 13. Februar 2019, 01:01

Hi DMD-OS

Bitte ausprobieren. Habe das Skript nur unter Ubuntu ausgetestet. Gewisse Zeilen habe ich für den Test auskommentiert!:

Code: Alles auswählen

import tkinter as tk

# Hier kann man den Text ändern... :)
master_text = "Guten Morgen, wie gehts?"
slave_text = "Hallo!"
from functools import partial

import tkinter as tk

APP_TITLE = "Feste Framegrösse"

APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 350
APP_HEIGHT = 400
           
class StartPage(tk.Frame):

    def __init__(self, parent):
        self.parent = parent
        tk.Frame.__init__(self, parent)

# Gruss wuf (Auskommentiert da ich es auf Ubuntu testet habe
#        self.parent.state('zoomed')
#        self.parent.overrideredirect(True)
#        self.parent.resizable(False, False)
#        self.parent.bind('<Map>', self.check_map)
#        self.parent.bind('<Unmap>', self.check_map)

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

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


    def start_MIDDLE(self):
        # FRAME: SIDEBAR - MIDDLE
        sidebar = tk.Frame(self.middle, width=28, bg="green")
        sidebar.pack(side=tk.LEFT, fill=tk.Y)

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

        # FRAME: TABLE 1 - MIDDLE
        frame_total = tk.Frame(frame_all, bg="blue")
        frame_total.propagate(False)
        frame_total.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=10, pady=10)

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

        # MATERTREE-Frame
        self.frame_mastertree = tk.Frame(frame_total, bg="pink")
        self.frame_mastertree.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=5, pady=5)
        self.start_MASTERTREE()

        # SLAVETREE-Frame
        self.frame_slavetree = tk.Frame(frame_fine, bg="pink")
        self.frame_slavetree.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=5, pady=5)
        self.start_SLAVETREE()

    def frame_all_config(self, event):
        width = event.widget.winfo_width()
        height = event.widget.winfo_height()

        print('Frame All Config', width, height)
        
    def start_MASTERTREE(self):
        border_master_text = tk.Frame(self.frame_mastertree)
        border_master_text.pack(side=tk.TOP, fill=tk.X)
        lb_master_var = tk.StringVar()
        label_mastertree = tk.Label(border_master_text, textvariable=lb_master_var, font=('Arial', 9, 'bold'), bg="darkgrey", relief=tk.FLAT, anchor=tk.W)
        label_mastertree.pack(fill=tk.X)
        border_master_tree = tk.Frame(self.frame_mastertree, bg="yellow")
        border_master_tree.pack(side=tk.TOP, fill=tk.BOTH)
        lb_master_var.set(master_text)


    def start_SLAVETREE(self):
        border_slave_text = tk.Frame(self.frame_slavetree)
        border_slave_text.pack(side=tk.TOP, fill=tk.X)
        lb_slave_var = tk.StringVar()
        label_slavetree = tk.Label(border_slave_text, textvariable=lb_slave_var, font=('Arial', 9, 'bold'), bg="darkgrey", relief=tk.FLAT,anchor=tk.W)
        label_slavetree.pack(fill=tk.X)
        border_slave_tree = tk.Frame(self.frame_slavetree, bg="yellow")
        border_slave_tree.pack(side=tk.TOP, fill=tk.BOTH)
        lb_slave_var.set(slave_text)

    def start_FOOTER(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.parent.quit).pack(pady=5)

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

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

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

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))
    
    start_page = StartPage(main_win)
    start_page.pack(expand=True, fill='both')

    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

Mittwoch 13. Februar 2019, 08:25

die lösung nach der ich seit ca. 1 woche suche: :twisted: :evil:

Code: Alles auswählen

frame_total.propagate(False)
frame_fine.propagate(False)
aber ich habe das jetzt auch verstanden :roll:
danke wuf!
Antworten