我正在使用python 3.7,无法拉伸标签以水平填充所有可用空间。
import tkinter
from tkinter import *
from time import sleep
root = tkinter.Tk()
root.geometry('500x600')
root.title("Test")
root.configure(background='black')
sym = tkinter.StringVar()
def symsel():
print(sym.get())
Labelgap1 = Label( root,bg="Green",height = 1)
Labelgap1.grid(row =0 ,column=0,sticky="nsew")
Labelgap1.grid_columnconfigure(0,weight=1)
Labelgap2 = Label( root,bg="Green",text = " Contracts",height = 2,relief = RAISED)
Labelgap2.grid(row =3 ,column=0,sticky="nsew")
Labelgap2.grid_columnconfigure(0,weight=1)
Radiobutton1 = tkinter.Radiobutton(root, bg = "black",text="Rdb1",fg="Green", variable=sym, value="Radiobutton1",font=("Courier", 15), command=symsel, tristatevalue=0)
Radiobutton1.grid(row=1,column=0,padx=2, pady= 5,sticky = "nsew")
Radiobutton2 = tkinter.Radiobutton(root,bg = "black", text="Radb2",fg="Green", variable=sym, value="Radiobutton2", font=("Courier", 15),command=symsel, tristatevalue=5)
Radiobutton2.grid(row=1,column=1,padx=2, pady= 5,sticky = "nsew")
Radiobutton3 = tkinter.Radiobutton(root,bg = "black", text="Rdb3",fg="Green", variable=sym, value="Radiobutton3", font=("Courier", 15),command=symsel, tristatevalue=5)
Radiobutton3.grid(row=1,column=2,padx=2, pady= 5,sticky = "nsew")
Radiobutton4 = tkinter.Radiobutton(root,bg = "black", text="Rdb4",fg="Green", variable=sym, value="Radiobutton4", font=("Courier", 15),command=symsel, tristatevalue=3)
Radiobutton4.grid(row=1,column=3,padx=2, pady= 5,sticky = "nsew")
Radiobutton5 = tkinter.Radiobutton(root,bg = "black", text="Rdb5",fg="Green", variable=sym, value="Radiobutton5", font=("Courier", 15),command=symsel, tristatevalue=1)
Radiobutton5.grid(row=2,column=0,padx=2, pady= 5,sticky = "nsew")
Radiobutton6 = tkinter.Radiobutton(root,bg = "black", text="Rdb6",fg="Green", variable=sym, value="Radiobutton6", font=("Courier", 15),command=symsel, tristatevalue=5)
Radiobutton6.grid(row=2,column=1,padx=2, pady= 5,sticky = "nsew")
Radiobutton7 = tkinter.Radiobutton(root,bg = "black", text="Rdb7",fg="Green", variable=sym, value="Radiobutton7", font=("Courier", 15),command=symsel, tristatevalue=4)
Radiobutton7.grid(row=2,column=2,padx=2, pady= 5,sticky = "nsew")
Radiobutton8 = tkinter.Radiobutton(root,bg = "black", text="Rdb8",fg="Green", variable=sym, value="Radiobutton7", font=("Courier", 15),command=symsel, tristatevalue=2)
Radiobutton8.grid(row=2,column=3,padx=2, pady= 5,sticky = "nsew")
root.grid_columnconfigure(0,weight = 1)
root.mainloop()
此代码输出:
当我将此GUI最大化到Windows屏幕时,它变为:
答案 0 :(得分:0)
您的标签在第0列中,并且有4列单选按钮。因此,当您执行grid_columnconfigure
时,标签将尽可能扩展,但是tkinter还必须适合其他三列。这就是为什么它们没有扩展到屏幕尺寸的原因。
为此,您可以将标签的columnspan
更改为4。
我还看到,您也在标签上使用了grid_columnconfigure。我希望您意识到这将不会有任何成就。
import tkinter as tk
root = tk.Tk()
root.geometry('500x600')
root.title("Test")
root.configure(background='black')
sym = tk.StringVar()
def symsel():
print(sym.get())
Labelgap1 = tk.Label(root, bg="Green", height = 1)
Labelgap1.grid(row =0 ,column=0, sticky="nsew", columnspan=4)
Labelgap2 = tk.Label(root, bg="Green", text = "Contracts", height = 2, relief = "raised")
Labelgap2.grid(row =3 ,column=0, sticky="nsew", columnspan=4)
Radiobutton1 = tk.Radiobutton(root, bg = "black",text="Rdb1",fg="Green", variable=sym, value="Radiobutton1",font=("Courier", 15), command=symsel, tristatevalue=0)
Radiobutton1.grid(row=1,column=0,padx=2, pady= 5,sticky = "nsew")
Radiobutton2 = tk.Radiobutton(root,bg = "black", text="Rdb2",fg="Green", variable=sym, value="Radiobutton2", font=("Courier", 15),command=symsel, tristatevalue=5)
Radiobutton2.grid(row=1,column=1,padx=2, pady= 5,sticky = "nsew")
Radiobutton3 = tk.Radiobutton(root,bg = "black", text="Rdb3",fg="Green", variable=sym, value="Radiobutton3", font=("Courier", 15),command=symsel, tristatevalue=5)
Radiobutton3.grid(row=1,column=2,padx=2, pady= 5,sticky = "nsew")
Radiobutton4 = tk.Radiobutton(root,bg = "black", text="Rdb4",fg="Green", variable=sym, value="Radiobutton4", font=("Courier", 15),command=symsel, tristatevalue=3)
Radiobutton4.grid(row=1,column=3,padx=2, pady= 5,sticky = "nsew")
Radiobutton5 = tk.Radiobutton(root,bg = "black", text="Rdb5",fg="Green", variable=sym, value="Radiobutton5", font=("Courier", 15),command=symsel, tristatevalue=1)
Radiobutton5.grid(row=2,column=0,padx=2, pady= 5,sticky = "nsew")
Radiobutton6 = tk.Radiobutton(root,bg = "black", text="Rdb6",fg="Green", variable=sym, value="Radiobutton6", font=("Courier", 15),command=symsel, tristatevalue=5)
Radiobutton6.grid(row=2,column=1,padx=2, pady= 5,sticky = "nsew")
Radiobutton7 = tk.Radiobutton(root,bg = "black", text="Rdb7",fg="Green", variable=sym, value="Radiobutton7", font=("Courier", 15),command=symsel, tristatevalue=4)
Radiobutton7.grid(row=2,column=2,padx=2, pady= 5,sticky = "nsew")
Radiobutton8 = tk.Radiobutton(root,bg = "black", text="Rdb8",fg="Green", variable=sym, value="Radiobutton7", font=("Courier", 15),command=symsel, tristatevalue=2)
Radiobutton8.grid(row=2,column=3,padx=2, pady= 5,sticky = "nsew")
root.grid_columnconfigure(0,weight = 1)
root.mainloop()
您可能还希望具有相同尺寸的单选按钮(现在不是,因为只有第0列正在扩展。其他列则没有。)为此,您可以将它们放在框架中,然后您不必再担心列跨度,因为所有列都将位于第零列。
import tkinter as tk
root = tk.Tk()
root.geometry('500x600')
root.title("Test")
root.configure(background='black')
sym = tk.StringVar()
def symsel():
print(sym.get())
Labelgap1 = tk.Label(root, bg="Green", height = 1)
Labelgap1.grid(row=0 ,column=0, sticky="nsew")
Labelgap2 = tk.Label(root, bg="Green", text = "Contracts", height = 2, relief = "raised")
Labelgap2.grid(row=2 ,column=0, sticky="nsew")
frame = tk.Frame(root)
frame.grid(row=1, column=0, sticky="nsew")
frame.grid_columnconfigure(0,weight = 1)
frame.grid_columnconfigure(1,weight = 1)
frame.grid_columnconfigure(2,weight = 1)
frame.grid_columnconfigure(3,weight = 1)
Radiobutton1 = tk.Radiobutton(frame, bg = "black",text="Rdb1",fg="Green", variable=sym, value="Radiobutton1",font=("Courier", 15), command=symsel, tristatevalue=0)
Radiobutton1.grid(row=0,column=0,padx=2, pady= 5,sticky = "nsew")
Radiobutton2 = tk.Radiobutton(frame,bg = "black", text="Rdb2",fg="Green", variable=sym, value="Radiobutton2", font=("Courier", 15),command=symsel, tristatevalue=5)
Radiobutton2.grid(row=0,column=1,padx=2, pady= 5,sticky = "nsew")
Radiobutton3 = tk.Radiobutton(frame,bg = "black", text="Rdb3",fg="Green", variable=sym, value="Radiobutton3", font=("Courier", 15),command=symsel, tristatevalue=5)
Radiobutton3.grid(row=0,column=2,padx=2, pady= 5,sticky = "nsew")
Radiobutton4 = tk.Radiobutton(frame,bg = "black", text="Rdb4",fg="Green", variable=sym, value="Radiobutton4", font=("Courier", 15),command=symsel, tristatevalue=3)
Radiobutton4.grid(row=0,column=3,padx=2, pady= 5,sticky = "nsew")
Radiobutton5 = tk.Radiobutton(frame,bg = "black", text="Rdb5",fg="Green", variable=sym, value="Radiobutton5", font=("Courier", 15),command=symsel, tristatevalue=1)
Radiobutton5.grid(row=1,column=0,padx=2, pady= 5,sticky = "nsew")
Radiobutton6 = tk.Radiobutton(frame,bg = "black", text="Rdb6",fg="Green", variable=sym, value="Radiobutton6", font=("Courier", 15),command=symsel, tristatevalue=5)
Radiobutton6.grid(row=1,column=1,padx=2, pady= 5,sticky = "nsew")
Radiobutton7 = tk.Radiobutton(frame,bg = "black", text="Rdb7",fg="Green", variable=sym, value="Radiobutton7", font=("Courier", 15),command=symsel, tristatevalue=4)
Radiobutton7.grid(row=1,column=2,padx=2, pady= 5,sticky = "nsew")
Radiobutton8 = tk.Radiobutton(frame,bg = "black", text="Rdb8",fg="Green", variable=sym, value="Radiobutton7", font=("Courier", 15),command=symsel, tristatevalue=2)
Radiobutton8.grid(row=1,column=3,padx=2, pady= 5,sticky = "nsew")
root.grid_columnconfigure(0,weight = 1)
root.mainloop()