【发布时间】:2019-12-15 03:33:15
【问题描述】:
有一个场景,我试图在一个框架内放置两个按钮,并将该框架放置在特定行的另一个框架中。但是放置在子框架内的按钮并没有扩展到全长,如下面的快照所示。
我尝试了网格、位置等,但仍然没有用。我没有尝试太多填充或填充等或设置按钮的一些默认大小。
尝试检查一些解决方案,他们一直说您需要行配置和列配置根小部件等,但即使这样也没有用。有什么建议如何确保小部件扩展到全长?
下面是我试过的代码:框架和按钮。
#!/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()
示例输出:当我将添加的两个按钮添加到单独的框架中,然后将其放在根框架中的行中时,想知道添加的两个按钮位于右上角。
新更新:我尝试了以下代码,但问题仍然相同。我做错了吗?
#!/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()
更新:已修复。它的列跨度。
【问题讨论】:
-
我尝试了网格、放置等,但仍然没有工作。您尝试使用什么代码?没用是什么意思?
-
从外观上看,您的
Frame没有展开而不是按钮。你需要展示你如何管理你的框架。 -
如果按钮似乎没有展开,可能是因为它们所在的框架没有展开。请提供minimal reproducible example。
-
对不起,我的错.. 忘了过去的代码。已经添加了一个例子。
标签: python python-2.7 tkinter