![](/img/trans.png)
[英]expanding frame inside canvas with scrollbar to full window width tkinter
[英]Widgets inside a Frame is not expanding to the full length placed inside another Frame in Tkinter
有一種情況,在這種情況下,我試圖將兩個按鈕放置在一個框架中,然后將該框架放置在特定行中的另一個框架中。 但是放置在子框架內的按鈕沒有擴展到全長,如下面的快照所示。
我嘗試使用網格,放置等,但仍然無法正常工作。 我在填充或填充等方面並沒有嘗試太多,也沒有設置按鈕的默認大小。
試圖檢查一些解決方案,他們一直說您需要對根小部件進行行配置和列配置等,但是即使這樣也不起作用。 有什么建議可以確保小部件擴展到全長嗎?
下面是我嘗試的代碼:對於框架和按鈕。
#!/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()
更新:已修復。 它的columnspan。
您可以使用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.