简体   繁体   English

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

[英]Widgets inside a Frame is not expanding to the full length placed inside another Frame in Tkinter

Have a scenario where in I am trying to place two button inside a Frame and place that frame in another Frame in a particular row. 有一种情况,在这种情况下,我试图将两个按钮放置在一个框架中,然后将该框架放置在特定行中的另一个框架中。 But the buttons placed inside the sub-frame is not expanding to the full length as shown in the snapshot below. 但是放置在子框架内的按钮没有扩展到全长,如下面的快照所示。

I tried with grid, place etc. but still didnt work. 我尝试使用网格,放置等,但仍然无法正常工作。 I haven't tried much on padding or filling etc. or setting some default size of the buttons. 我在填充或填充等方面并没有尝试太多,也没有设置按钮的默认大小。

在此处输入图片说明

Tried checking for some solution they kept saying you need to rowconfigure and columnconfigure the root widget etc. but even that didnt work. 试图检查一些解决方案,他们一直说您需要对根小部件进行行配置和列配置等,但是即使这样也不起作用。 Any suggestion how to make sure the widgets expand to full length ? 有什么建议可以确保小部件扩展到全长吗?

Below is the code I tried: For frame and the buttons. 下面是我尝试的代码:对于框架和按钮。

#!/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()

Example output: Wondering by the two buttons added sits in the right corner when I added them in a separate frame and then placed it in row in the root frame. 输出示例:当我将两个按钮添加到单独的框架中,然后将其放置在根框架的行中时,它们位于右上角。

在此处输入图片说明

NEW UPDATE: I tried the below code but still the problem is same. 新更新:我尝试下面的代码,但问题仍然相同。 Am i doing something wrong ? 难道我做错了什么 ?

#!/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()

在此处输入图片说明

UPDATE : Got it fixed. 更新:已修复。 Its the columnspan. 它的columnspan。

You can expand the columns in the frame ib_frame with columnconfigure : 您可以使用columnconfigure扩展框架ib_frame的列:

# 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM