0

我正在使用 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 屏幕时,它变为: 吉美思

4

1 回答 1

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() 

在此处输入图像描述


您可能还希望拥有相同尺寸的单选按钮(现在它们不是,因为只有第零列正在扩展。其他列不会。)。为此,您可以将它们放在一个框架中,然后您不必再担心列跨度,因为所有列都将在第零列中。

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()

在此处输入图像描述

于 2019-01-06T09:17:09.477 回答