簡體   English   中英

Tkinter:create_window()和框架無法正確顯示

[英]Tkinter: create_window() and frame not displayed correctly

我正在使用Python和Tkinter進行一個小項目。 我需要顯示一張表,其中包含一些挖掘機/挖掘機的所有功能。

表格基本上是一個大框架,里面有很多標簽對象。 我將此框架放在Canvas對象中(使用create_window() ),該對象位於另一個框架中(與滾動條一起)

問題是我無法正確顯示表格。 我只能得到它的垂直部分:

在此處輸入圖片說明

綠色部分是包含Canvas對象和滾動條的框架

我發現解決該問題的唯一方法是使用固定寬度...但這不是很好。 有什么建議么?

編輯:

我希望桌子具有固定的高度( 這正在工作 ),如您在此處看到的:

在此處輸入圖片說明

紅色部分是包含表的canvas對象,綠色部分是包含畫布和滾動條的框架。 我刪除了滾動條,以便更輕松地了解發生了什么。

問題在於canvas對象沒有展開( 請參見上一個屏幕截圖 ,我不知道為什么。 我想擁有的是畫布的寬度跟隨桌子的寬度。

代碼在這里:

#!/usr/bin/env python
# -*- coding: utf-8 -*- 

import Tkinter, string, table
from functions import *

bg_color = "#d3d3d3"
first_table = True

# MODEL: Functions to handle the main processing of the program in
# response to user interaction
def create_tables():
    global tab, first_table, tab_space, mbtext
    select = choice.get()
    choices = [ 1, 2, 3, 4, 5]
    mbtext.set( str( choices[select])) # resetting the number in the menubutton
    if not first_table:
        # cleaning the canvas
        t.destroy()
        comp_space.destroy()
    # space to display the objects
    master = Tkinter.Frame( top, bg = 'green')
    master.grid( row = 2, column = 0, sticky = "wesn", padx = 10  )

    # space for the table:
    tab_space = Tkinter.Canvas( master, bg = 'red', highlightthickness = 0, height = 600, scrollregion=(0, 0, 2000, 780))
    tab_space.grid( row = 0, column = 0)

    # creating the table...
    tab = table.CompTable( tab_space, columns = choices[select]+1 )
    tab.first_set()
    tab_space.create_window(0, 0, anchor = "nw", window = tab)

    # and the scrollbar:
    scrollY = Tkinter.Scrollbar ( master, bg = bg_color, bd = 4, activebackground = bg_color, orient = "vertical")
    scrollY.grid( row = 0, column = 1, sticky = "nswe")

    #binding canvas and scrollbar together
    scrollY.configure( command = tab_space.yview)
    tab_space.configure(yscrollcommand = scrollY.set )

# VIEW: Setup the widgets

# The main window
top = Tkinter.Tk()
top.configure( bg = bg_color)
top.title("Comparatore")

# logo_frame/canvas - using a Canvas object to load images
logo_canvas = Tkinter.Canvas( top,  bg = bg_color, highlightthickness = 0, height = 58, width = 590 )
logo_canvas.grid( row = 0, column = 0, ipadx = 0, ipady=0, sticky = "nw")
logo = Tkinter.PhotoImage(file = "Images/Logo.gif")
logo_canvas.create_image(0, 0, image = logo, anchor="nw")

# background
bg_label = Tkinter.Label( top, bg = bg_color )
bg_label.grid( row = 1, column = 0, sticky = "nesw")

# menu to handle how many items we are using
select_text = Tkinter.Label( bg_label, text = " Selezionare il numero di macchine da confrontare: ", 
        font = ("verdana", 16), bg = bg_color)
select_text.grid( row = 0, column = 0, sticky = "nsew")

mbtext = Tkinter.StringVar()
mbtext.set("")
how_many_mb = Tkinter.Menubutton( bg_label, textvariable = mbtext, relief= "raised", bg = bg_color)
how_many_mb.menu = Tkinter.Menu( how_many_mb, tearoff = 0)
how_many_mb["menu"] = how_many_mb.menu
how_many_mb.grid( row = 0, column = 1, sticky = "nsew", padx = 4, ipadx = 18)

# CONTROLLER
choice = Tkinter.IntVar()
how_many_mb.menu.add_radiobutton( label = "1", variable = choice, value = 0, command = create_tables)
how_many_mb.menu.add_radiobutton( label = "2", variable = choice, value = 1, command = create_tables)
how_many_mb.menu.add_radiobutton( label = "3", variable = choice, value = 2, command = create_tables)
how_many_mb.menu.add_radiobutton( label = "4", variable = choice, value = 3, command = create_tables)
how_many_mb.menu.add_radiobutton( label = "5", variable = choice, value = 4, command = create_tables)


##
Tkinter.mainloop()

這是表格模塊的代碼:

#!/usr/bin/env python
# -*- coding: utf-8 -*- 

import Tkinter as tk

mbtext1 = tk.StringVar()
mbtext1.set("-Selezionare-")


details = ["Costruttore", "Modello", "Capacità Benna", "Carico rib. art.", "Peso", "Potenza",
                "Motore (Marca)", "Cilindrata", "Cilindri", "Alesaggio per corsa", "Regime di taratura",
                "Alimentazione aria", "Pompe", "Portata", "Pressione", "Trasmissione", "Marce",
                "Velocità traslazione", "Velocità di rotazione", "Differenziali", "Freni", "Pneumatici", "Passo", "Carreggiata",
                "Articolazione", " Raggio sterzo alla benna ", "Cinematismo benna", "Max altezza perno b.",
                "Forza di strappo", "Forza di penetrazione", "Sbalzo posteriore torretta", "Lama", "Larghezza benna", 
                "Larghezza max", "Altezza trasporto", "Larghezza cingoli", "Larghezza torretta",
                "Larghezza esterna pneumatici", "Lunghezza trasporto"]

class CompTable(tk.Frame):
    global details

    def __init__(self, parent, rows=len(details), columns=2):
        # using black background
        tk.Frame.__init__(self, parent, background="black")
        self._widgets = []
        for row in range(rows):
            current_row = []
            for column in range(columns):
                if row in [ 0, 1] and column != 0:
                    menu = tk.Menubutton(self, textvariable = mbtext1, width = 15)
                    menu.grid( row=row, column=column, sticky="nsew", padx=1, pady=1 )
                    current_row.append(menu)
                else:
                    label = tk.Label(self, background = "#fcfcfc", text="                           ", 
                                     borderwidth=0)
                    label.grid( row=row, column=column, sticky="nsew", padx=1, pady=1)
                    current_row.append(label)
            self._widgets.append(current_row)

    def set(self, row, column, value):
        widget = self._widgets[row][column]
        widget.configure(text=value)

    def first_set( self ):
        actual_detail = 0
        for element in details:
            self.set(actual_detail, 0, element)
            actual_detail += 1

嘗試將其添加到表中,然后在tab_space.grid( row = 0, column = 0)之后創建代碼:

master.columnconfigure(0, weight=1)

編輯

好的,這或多或少地解決了上述問題:

如果您更換

tab_space.grid( row = 0, column = 0)

tab_space.grid( row = 0, column = 0, sticky="nsew")
top.columnconfigure(0, weight=1)
master.columnconfigure(0, weight=1)

說明:

Tkinter grid幾何管理器對小部件中的每一列和每一行都有一個“權重”。 列/行的權重決定了列或行占用窗口小部件分配的空間。

您可以更改給定列/行的權重。 例如,如果您有一個名為frame ,則frame.columnconfigure(3, weight=7) frame窗口小部件的第3列的權重設置為7。有一個對應的函數稱為rowconfigure

默認權重為0,這意味着隨着可用空間或所需空間的增加或減少,列/行不會增長或收縮-它們是靜態的。 如果第0列的權重為2,第1列的權重為3,並且有5個額外像素可用,則第0列將擴大2個像素,而第1列將擴大3個像素。

如果只有一列,則最后一點無關緊要,但是此問題的重要部分是包含表格畫布的列(以及包含主框架的列)的權重非零 ,因此網格幾何管理器知道使其完全能夠擴展或收縮,而不是保持其最初分配的寬度。

這是有關NMT的columnconfigurerowconfigure函數的一些文檔 興趣點: columnconfigurerowconfigure實際上是grid_columnconfiguregrid_rowconfigure別名,我假設要實現以節省5次擊鍵。

筆記:

通過手動調整窗口大小,您應該能夠看到表的其余部分。 我認為您將必須使用<Configure>事件來做一些工作,以使窗口自動調整為正確的寬度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM