【问题标题】:Python 3 Radio button controlling label textPython 3单选按钮控制标签文本
【发布时间】:2018-05-29 11:35:00
【问题描述】:

我正在学习 Python3 和更多必需品,即 TkInter GUI 方面。当我遇到这个问题时,我正在阅读 James Kelly 的一本书。他所有的例子都用标签/画布/复选框等创建了一个新窗口,似乎工作正常。

但由于我想在更真实的场景中进行实验,所以我将大部分内容放在一个窗口上。这是我遇到问题的地方。我无法在框架中使用单选按钮来更改父窗口中标签的措辞。

完整的代码是:-

#! /usr/bin/python3

from tkinter import *

def win_pos(WL,WH,xo=0,yo=0) :

# Screen size & position procedure
# Screen size
    SW = home.winfo_screenwidth()
    SH = home.winfo_screenheight()
# 1/2 screen size
    sw=SW/2
    sh=SH/2
# 1/2 window size
    wl=WL/2
    wh=WH/2
# Window position
    WPx=sw-wl+xo
    WPy=sh-wh+yo
# Resulting string
    screen_geometry=str(WL) + "x" + str(WH) + "+" + str(int(WPx)) + "+" \     + str(int(WPy))
    return screen_geometry

# Create a window 
home=Tk()
home.title("Radio buttons test")
# Set the main window
home.geometry(win_pos(600,150))

lab1=Label(home)

lab1.grid(row=1,column=1)

fraym1=LabelFrame(home, bd=5, bg="red",relief=SUNKEN, text="Label frame text")

fraym1.grid(row=2,column=2)

laybl1=Label(fraym1, text="This is laybl1")

laybl1.grid(row=0, column=3)

var1=IntVar()

R1=Radiobutton(fraym1, text="Apple", variable=var1, value=1)
R1.grid(row=1, column=1)

R2=Radiobutton(fraym1, text="Asus", variable=var1, value=2)
R2.grid(row=1, column=2)

R3=Radiobutton(fraym1, text="HP", variable=var1, value=3)
R3.grid(row=1, column=3)

R4=Radiobutton(fraym1, text="Lenovo", variable=var1, value=4)
R4.grid(row=1, column=4)

R5=Radiobutton(fraym1, text="Toshiba", variable=var1, value=5)
R5.grid(row=1, column=5)

# Create function used later
def sel(var) :

    selection="Manufacturer: "

    if var.get() > 0 : 

        selection=selection + str(var.get())

    lab1.config(text=selection)

R1.config(command=sel(var1))
R2.config(command=sel(var1))
R3.config(command=sel(var1))
R4.config(command=sel(var1))
R5.config(command=sel(var1))

R1.select()

mainloop()

我意识到使用类/函数还有改进的余地,但我需要先在脑海中解决这个问题,然后再继续。正如可以看到的那样,我不是一个完全的编程新手,但这是我的头脑。

能否给出解决方案以及解决方案背后的原因?

【问题讨论】:

标签: python-3.x tkinter radio-button label


【解决方案1】:

您可以通过分配相同的变量类对象var1 作为其textvariable 选项来修改标签的文本,但由于lab1 的文本略有不同,请尝试删除:

R1.config(command=sel(var1))
R2.config(command=sel(var1))
R3.config(command=sel(var1))
R4.config(command=sel(var1))
R5.config(command=sel(var1))

R1.select()

并将sel修改为:

def sel(*args) :

    selection="Manufacturer: "

    selection=selection + str(var1.get())

    lab1.config(text=selection)

然后在mainloop 之前的某个地方调用var1.trace("w", sel),如下所示:

...
var1.trace("w", sel)
mainloop()

还有一个简单的例子:

import tkinter as tk

root = tk.Tk()

manufacturers = ["man1", "man2", "man3", "man4", "man5"]

lbl = tk.Label(root, text="Please select a manufacturer.")
lbl.pack()

# create an empty dictionary to fill with Radiobutton widgets
man_select = dict()
# create a variable class to be manipulated by radiobuttons
man_var = tk.StringVar(value="type_default_value_here_if_wanted")

# fill radiobutton dictionary with keys from manufacturers list with Radiobutton
# values assigned to corresponding manufacturer name
for man in manufacturers:
    man_select[man] = tk.Radiobutton(root, text=man, variable=man_var, value=man)
    #display
    man_select[man].pack()


def lbl_update(*args):
    selection="Manufacturer: "
    selection=selection + man_var.get()

    lbl['text'] = selection

#run lbl_update function every time man_var's value changes
man_var.trace('w', lbl_update)
root.mainloop()

标签的与单选按钮的值相同的示例:

import tkinter as tk

root = tk.Tk()

# radiobutton group will the button selected with the value=1
num = tk.IntVar(value=1)

lbl = tk.Label(root, textvariable=num)

zero = tk.Radiobutton(root, text="Zero", variable=num, value=0)
one = tk.Radiobutton(root, text="One", variable=num, value=1)

#display
lbl.pack()
zero.pack()
one.pack()

root.mainloop()

【讨论】:

  • 感谢 Nae 的输入,但您的建议导致:- TypeError: sel() 采用 0 个位置参数,但给出了 3 个
  • @John_S 很抱歉你是对的。您需要将def sel(): 更改为def sel(*args):,因为trace 也会向函数发送一些内部参数。我更新了我的答案以进行更正。
  • 感谢 Nae,现在一切正常。刚刚了解了跟踪,以及 (*args) 的含义。约翰
猜你喜欢
  • 1970-01-01
  • 2014-11-11
  • 2015-12-22
  • 2013-07-20
  • 2017-05-23
  • 2015-11-24
  • 1970-01-01
  • 2018-02-06
相关资源
最近更新 更多