一个框架内的小部件未扩展到Tkinter中另一个框架内的全长

时间:2019-08-07 13:15:17

标签: python python-2.7 tkinter

有一种情况,在这种情况下,我试图将两个按钮放置在一个框架中,然后将该框架放置在特定行中的另一个框架中。但是放置在子框架中的按钮没有扩展到全长,如下面的快照所示。

我尝试了网格,放置等,但是仍然没有用。我在填充或填充等方面并没有尝试太多,也没有设置按钮的默认大小。

enter image description here

试图检查一些解决方案,他们一直说您需要对根窗口小部件进行行配置和列配置等,但即使这样也不起作用。有什么建议可以确保小部件扩展到全长吗?

下面是我尝试的代码:用于框架和按钮。

#!/usr/intel/bin/python2.7

import Tkinter
import Tkinter as tk
from Tkinter import * 
import ttk
import Tkinter
from Tkinter import *
import subprocess
import shlex
import os 
#from PIL import Image, ImageTk
#import Image, ImageTk
import time
import string
import tkFont
import ttk

class MyApp:
    def __init__(self, parent):
        self.rel3 = None
        self.ib_frame = None
        self.rb =  None
        self.eb = None

        if not (self.rel3):
            self.rel3 = Label(root, font=MyFontH2, text="What Type Of Widget You Wanted To Create: ")
            self.rel3.grid(row=6, column=0, sticky='W', padx=38)
            self.rel3.rowconfigure(6,weight=1)
        else:
            self.rel3.grid()

        # Frame Code
        if not (self.ib_frame):   
            self.ib_frame = Frame(root)
            self.ib_frame.grid(row=7, column=0, columnspan=1, sticky='WE')
            self.ib_frame.rowconfigure(7,weight=1)
            #self.ib_frame.columnconfigure(0,weight=1)

        # Button1 Code
        if not self.rb: 
            self.rb = Button(self.ib_frame, background="royalblue1", activebackground="blue2", text="RTMM")    
            self.rb.grid(row=0, column=0, sticky='WE')
            self.rb.rowconfigure(0, weight=1)
            # self.rb.pack(side=LEFT, fill=BOTH)

        # Button2 Code
        if not (self.eb):     
            self.eb = Button(self.ib_frame, background="orangered", activebackground="orangered3", text="ECG")
            self.eb.grid(row=0, column=1, sticky='WE')
            self.eb.rowconfigure(0, weight=1)
            #self.eb.pack(side=RIGHT, fill=BOTH) 

root = Tk()
root.title("Test UI")

MyFontH2 = tkFont.Font(family='courier', size=20, weight=tkFont.BOLD)

myapp = MyApp(root)
root.mainloop()

示例输出:当我将两个按钮添加到单独的框架中,然后将其放置在根框架的行中时,位于右上角的按钮使我们感到困惑。

enter image description here

新更新:我尝试了以下代码,但问题仍然相同。我做错什么了吗?

#!/usr/intel/bin/python2.7

import Tkinter
import Tkinter as tk
from Tkinter import * 
import ttk
import Tkinter
from Tkinter import *
import subprocess
import shlex
import os 
#from PIL import Image, ImageTk
#import Image, ImageTk
import time
import string
import tkFont
import ttk

class MyApp:
    def __init__(self, parent):
        self.rel3 = None
        self.ib_frame = None
        self.rb =  None
        self.eb = None
        self.fb = None
        self.l_1 = None
        self.e_1 = None


        if not (self.l_1):
            self.l_1 = Label(text="Choose the Config: ")
            self.l_1.grid(row=1, column=0, sticky='W')
            self.l_1.rowconfigure(1,weight=1)
            self.l_1.columnconfigure(0,weight=1)
        else:
            self.l_1.grid_forget(); self.l_1 = None
            self.l_1 = Label(text="Choose the Config: ")
            self.l_1.grid(row=1, column=0, sticky='W')
            self.l_1.rowconfigure(1,weight=1)
            self.l_1.columnconfigure(0,weight=1)

        if not (self.e_1):    
            self.e_1 = Entry(bg="goldenrod")
            self.e_1.grid(row=1, column=1, sticky='WE')
            self.e_1.rowconfigure(1,weight=1)
            self.e_1.columnconfigure(1,weight=1)
        else:
            self.e_1.grid_forget(); self.e_1 = None
            self.e_1 = Entry()
            self.e_1.grid(row=1, column=1, sticky='WE')
            self.e_1.rowconfigure(1,weight=1)
            self.e_1.columnconfigure(1,weight=1)


        # Frame Code
        if not (self.ib_frame):   
            self.ib_frame = Frame(root)
            self.ib_frame.grid(row=7, column=0, columnspan=**2**, sticky='WE')
            #self.ib_frame.rowconfigure(7,weight=1)
            self.ib_frame.columnconfigure((0,1,2),weight=1)

        # Button1 Code
        if not self.rb: 
            self.rb = Button(self.ib_frame, background="royalblue1", activebackground="blue2", text="VEGI")    
            self.rb.grid(row=0, column=0, sticky='WE')
            #self.rb.rowconfigure(0, weight=1)
            self.rb.columnconfigure(0,weight=1)
            # self.rb.pack(side=LEFT, fill=BOTH)

        # Button2 Code
        if not (self.eb):     
            self.eb = Button(self.ib_frame, background="orangered", activebackground="orangered3", text="DESERT")
            self.eb.grid(row=0, column=1, sticky='WE')
            #self.eb.rowconfigure(0, weight=1)
            self.eb.columnconfigure(1,weight=1)
            #self.eb.pack(side=RIGHT, fill=BOTH)

        # Button2 Code
        if not (self.fb):     
            self.fb = Button(self.ib_frame, background="tan1", activebackground="tan4", text="FRUIT")
            self.fb.grid(row=0, column=2, sticky='WE')
            #self.fb.rowconfigure(0, weight=1)
            self.fb.columnconfigure(2,weight=1)
            #self.fb.pack(side=RIGHT, fill=BOTH)     

root = Tk()
root.title("Test UI")

MyFontH2 = tkFont.Font(family='courier', size=20, weight=tkFont.BOLD)

myapp = MyApp(root)
root.mainloop()

enter image description here

更新:已修复。它的列跨度。

1 个答案:

答案 0 :(得分:0)

您可以使用ib_frame扩展框架columnconfigure中的列:

# Frame Code
if not (self.ib_frame):   
    self.ib_frame = Frame(root, bg='tan')
    self.ib_frame.grid(row=7, column=0, columnspan=1, sticky='WE')
    self.ib_frame.rowconfigure(7, weight=1)
    self.ib_frame.columnconfigure([0,1], weight=1)
    #                               ^
    #                      expand both columns