2017-12-31 64 views
-1

Ich verwende diesen Code von heretkinter Gitter und vererbte Objekt

Ich versuche, eine einfache Tabelle in einem Frame in tkinter einzufügen. Ich habe den Code angepasst, aber ich kann nicht scheinen, dass das neue Objekt die Rastergröße berücksichtigt.

Die Tabelle sollte die volle Breite haben und die Schaltfläche sollte angezeigt werden, aber die Tabelle nimmt nur die Hälfte des Fensters auf und ihre Höhe scheint festgelegt, da wenn ich die Geometrie der Hauptrahmen Geometrie ändert die Schaltfläche zeigt.

Kann mir jemand sagen, was ich falsch mache oder eine bessere Alternative vorschlagen, damit ich den Beginn des nächsten Jahres schlug meinen Kopf gegen die Wand nicht verbringen müssen :)

from tkinter import * 
from tkinter import ttk 
import tkinter.font as tkFont 

class frmListMembers(Frame): 

    def __init__(self, master): 

     Frame.__init__(self, master) 
     #self.grid() 

     self.master = master 
     self.master.title("List Members") 
     self.master.geometry("500x240+0+0")   
     self.master.resizable(False, False)   
     self.focus_set() 

     container = Frame(master) 
     container.grid(row=0, column=0, padx=10,pady=10) 
     container.grid_columnconfigure(0, weight=1) 
     self.tblMembers = MemberListBox(container) #.grid(row=0,column=0) 

     self.OkButton = Button(master, text="DONE", command=self.oKButtonClick).grid(row=2, column=0, pady=(5), padx=10, sticky="E") 
     container.grid_rowconfigure(2, weight=1) 



    def oKButtonClick(self): 

     self.master.destroy() 


class MemberListBox(object): 
    """use a ttk.TreeView as a multicolumn ListBox""" 
    def __init__(self, container): 
     self.container = container 
     self.tree = None 
     self._setup_widgets() 
     self._build_tree() 


    def _setup_widgets(self): 

     #container = Frame() 
     #container.pack(fill='both', expand=True) 

     # create a treeview with vertical scrollbar 
     self.tree = ttk.Treeview(columns=car_header, show="headings") 
     vsb = Scrollbar(orient="vertical", command=self.tree.yview) 
     self.tree.configure(yscrollcommand=vsb.set, xscrollcommand=None) 
     self.tree.grid(column=0, row=0, sticky='NSEW', in_=self.container) 
     vsb.grid(column=1, row=0, sticky='NS', in_=self.container) 

     #self.container.grid_columnconfigure(0, weight=1) 
     #self.container.grid_rowconfigure(0, weight=1) 

    def _build_tree(self): 
     for col in car_header: 
      self.tree.heading(col, text=col.title(), 
      command=lambda c=col: sortby(self.tree, c, 0)) 
      # adjust the column's width to the header string 
      self.tree.column(col,width=tkFont.Font().measure(col.title())) 

     for item in car_list: 
      self.tree.insert('', 'end', values=item) 
      # adjust column's width if necessary to fit each value 
      for ix, val in enumerate(item): 
       col_w = tkFont.Font().measure(val) 
       if self.tree.column(car_header[ix],width=None)<col_w: 
        self.tree.column(car_header[ix], width=col_w) 


# the test data ... 
car_header = ['car', 'repair', 'price', 'Mileage'] 
car_list = [ 
('Hyundai', 'brakes', 10999, 20101) , 
('Honda', 'light', 10999, 20101) , 
] 
root = Tk() 
frmListMembers(root) 
root.mainloop() 
+1

Bitte korrigieren Sie Ihren Einzug, damit wir Ihren Code testen können. – Novel

+0

Bitte geben Sie ein [a _Minimal_, Complete und Verifable Beispiel] (https://Stackoverflow.com/help/mcve) oder Sie sollten beginnen, für einen [Kopfschutz] zu speichern (https://www.brownhealthcare.com/images /HeadSaver%20head%20protector%20.jpg). – Nae

+0

@Nae - Es ist so minimal und vollständig, wie ich bekommen kann. Was ist falsch daran, abgesehen von der Frage, auf die sich Roman bezieht? – grayson

Antwort

0

Zu allererst ich habe versucht, a Minimal, Complete, and Verifiable example von OP Code zu meinen besten Bemühungen zu schaffen, während noch den Code zu bewahren:

from tkinter import * 
from tkinter import ttk 
import tkinter.font as tkFont 

class frmListMembers(Frame): 

    def __init__(self, master): 

     Frame.__init__(self, master) 

     self.master = master 
     self.master.geometry("500x240+0+0")   
     self.master.resizable(False, False) 

     container = Frame(master) 
     container.grid(row=0, column=0, padx=10,pady=10, sticky='ew') 
     self.tblMembers = MemberListBox(container) #.grid(row=0,column=0) 

     Button(master, text="DONE").grid(row=2, column=0, pady=(5), padx=10, sticky="E") 


class MemberListBox(object): 
    """use a ttk.TreeView as a multicolumn ListBox""" 
    def __init__(self, container): 
     self.container = container 
     self.tree = None 
     self._setup_widgets() 
     self._build_tree() 


    def _setup_widgets(self): 

     self.tree = ttk.Treeview(columns=car_header, show="headings") 
     vsb = Scrollbar(orient="vertical", command=self.tree.yview) 
     self.tree.configure(yscrollcommand=vsb.set, xscrollcommand=None) 
     self.tree.grid(column=0, row=0, sticky='NSEW', in_=self.container) 
     vsb.grid(column=1, row=0, sticky='NS', in_=self.container) 

    def _build_tree(self): 
     for col in car_header: 
      self.tree.heading(col, text=col.title()) 
      # adjust the column's width to the header string 
      self.tree.column(col,width=tkFont.Font().measure(col.title())) 

     for item in car_list: 
      self.tree.insert('', 'end', values=item) 
      # adjust column's width if necessary to fit each value 
      for ix, val in enumerate(item): 
       col_w = tkFont.Font().measure(val) 
       if self.tree.column(car_header[ix],width=None)<col_w: 
        self.tree.column(car_header[ix], width=col_w) 


# the test data ... 
car_header = ['car', 'repair', 'price', 'Mileage'] 
car_list = [ 
('Hyundai', 'brakes', 10999, 20101) , 
('Honda', 'light', 10999, 20101) , 
] 
root = Tk() 
frmListMembers(root) 
root.mainloop() 

oben Code ich habe ersetzt ab:

container.grid(row=0, column=0, padx=10,pady=10, sticky='ew') 

Linie unten an der Linie:

class MemberListBox(object): 

mit:

container.grid(row=0, column=0, padx=10,pady=10, sticky='nsew') # expand 
self.tblMembers = MemberListBox(container) 
container.grid_rowconfigure(0, weight=1)  # allow tree to resize height 
container.grid_columnconfigure(0, weight=1)  # allow tree to resize width 
#container.grid_columnconfigure(1, weight=1) # don't allow scrollbar to rsz 
self.master.grid_columnconfigure(0, weight=1) # allow container to resize 
self.master.grid_rowconfigure(0, weight=1)  # both its width and height 

self.OkButton = Button(container, text="DONE") # put button in container 
self.OkButton.grid(row=2, column=0, pady=(5), padx=10, sticky="E") 

, die in Folge:

from tkinter import * 
from tkinter import ttk 
import tkinter.font as tkFont 

class frmListMembers(Frame): 

    def __init__(self, master): 

     Frame.__init__(self, master) 

     self.master = master 
     self.master.geometry("500x240+0+0")   
     self.master.resizable(False, False) 

     container = Frame(master) 
     container.grid(row=0, column=0, padx=10,pady=10, sticky='nsew') # expand 
     self.tblMembers = MemberListBox(container) 
     container.grid_rowconfigure(0, weight=1)  # allow tree to resize height 
     container.grid_columnconfigure(0, weight=1)  # allow tree to resize width 
     #container.grid_columnconfigure(1, weight=1) # don't allow scrollbar to rsz 
     self.master.grid_columnconfigure(0, weight=1) # allow container to resize 
     self.master.grid_rowconfigure(0, weight=1)  # both its width and height 

     self.OkButton = Button(container, text="DONE") # put button in container 
     self.OkButton.grid(row=2, column=0, pady=(5), padx=10, sticky="E") 


class MemberListBox(object): 
    """use a ttk.TreeView as a multicolumn ListBox""" 
    def __init__(self, container): 
     self.container = container 
     self.tree = None 
     self._setup_widgets() 
     self._build_tree() 


    def _setup_widgets(self): 

     self.tree = ttk.Treeview(columns=car_header, show="headings") 
     vsb = Scrollbar(orient="vertical", command=self.tree.yview) 
     self.tree.configure(yscrollcommand=vsb.set, xscrollcommand=None) 
     self.tree.grid(column=0, row=0, sticky='NSEW', in_=self.container) 
     vsb.grid(column=1, row=0, sticky='NS', in_=self.container) 

    def _build_tree(self): 
     for col in car_header: 
      self.tree.heading(col, text=col.title()) 
      # adjust the column's width to the header string 
      self.tree.column(col,width=tkFont.Font().measure(col.title())) 

     for item in car_list: 
      self.tree.insert('', 'end', values=item) 
      # adjust column's width if necessary to fit each value 
      for ix, val in enumerate(item): 
       col_w = tkFont.Font().measure(val) 
       if self.tree.column(car_header[ix],width=None)<col_w: 
        self.tree.column(car_header[ix], width=col_w) 


# the test data ... 
car_header = ['car', 'repair', 'price', 'Mileage'] 
car_list = [ 
('Hyundai', 'brakes', 10999, 20101) , 
('Honda', 'light', 10999, 20101) , 
] 
root = Tk() 
frmListMembers(root) 
root.mainloop() 

Hinweis: Es gibt nicht ein einziges Teil dieser 'Antwort' wouldn‘ Es war viel einfacher, wenn das OP ein minimaleres Beispiel lieferte.