2017-12-31 73 views
-1

私はTkinterでは、フレームに簡単なテーブルを挿入しようとしています。コードを修正しましたが、新しいオブジェクトがグリッドサイズを尊重するように見えます。Tkinterのグリッドと継承されたオブジェクト

テーブルが全幅でボタンが表示されるはずですが、テーブルがウィンドウの半分を占めるだけで、その高さが固定されているように見えます。

私は壁に私の頭を叩いて来年の開始を費やす必要はありませんので、誰もが私が間違っているのを教えたり、より良い代替案を提案することができます:)すべての

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

コードをテストできるようにインデントを修正してください。 – Novel

+0

[Minimal_、Complete、Verifiableの例](https://stackoverflow.com/help/mcve)を入力するか、[head protector](https://www.brownhealthcare.com/images)に保存してください。 /HeadSaver%20head%20protector%20.jpg)。 – Nae

+0

@Nae - それは私が得ることができるように最小限で完全です。小説に言及した以外の問題は何ですか? – grayson

答えて

0

まず、

container.grid(row=0, column=0, padx=10,pady=10, sticky='ew') 
012:コードの上に使用

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() 

私から交換しました:私はまだあなたのコードを維持しながら、私の努力を最大限にOPのコードのa Minimal, Complete, and Verifiable exampleを作成しようとしました

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() 

注:が生じ

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): 

:下の行に

ラインこの '答え' wouldn」は、単一の部分はありませんOPがもっと小さな例を提供していれば、ずっと簡単でした。

関連する問題