单选按钮未设置值
radiobutton not setting value
我正在制作一个文本编辑程序,但是当我 运行 这个代码
它不起作用,因为在第 13 行,selecfont.get() 没有返回任何值。
我不知道这是否与 for 循环有关,但我无法修复它
我在另一个程序上遇到了类似的问题。
谢谢
from tkinter import *
from os import listdir
from os import path
from threading import Thread
def open():
print("open selected")
def save():
print("save selected")
def fontchange():
sheet.config(font = (str(selecfont.get())))
print(selecfont.get())
fonts = ["Arial","Courier New","Times New Roman","Verdana",
"System","Impact","Fixedsys","corbel"]
master = Tk()
master.config(bg = "#080d31")
master.title("Ed.co")
menubar = Menu(master,background = "#080d31")
filemenu = Menu(menubar,tearoff=0)
filemenu.add_command(label = "open",command = open)
filemenu.add_command(label = "save",command = save)
menubar.add_cascade(label = "file",menu = filemenu)
settingsmenu = Menu(menubar,tearoff=0)
fontmenu = Menu(settingsmenu,tearoff=0)
settingsmenu.add_cascade(label = "font",menu = fontmenu)
menubar.add_cascade(label = "settings",menu = settingsmenu)
for font in fonts:
selecfont = StringVar()
fontmenu.add_radiobutton(value = font,label = font,variable = selecfont,command = fontchange)
sheet = Text(master,width = 71,height = 15,font = "corbel")
sheet.grid(row=1,column=0,columnspan = 3)
master.config(menu = menubar)
master.mainloop()
错误信息:
sheet.config(font = (str(selecfont.get())))
File "C:\Python34\lib\tkinter\__init__.py", line 1319, in configure
return self._configure('configure', cnf, kw)
File "C:\Python34\lib\tkinter\__init__.py", line 1310, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: font "" doesn't exist
单选按钮需要共享一个公共变量,但您正在为每个单选按钮创建一个新变量。您需要将 selecFont
的定义移到循环之外:
selecFont = StringVar()
for font in fonts:
...
我正在制作一个文本编辑程序,但是当我 运行 这个代码 它不起作用,因为在第 13 行,selecfont.get() 没有返回任何值。 我不知道这是否与 for 循环有关,但我无法修复它 我在另一个程序上遇到了类似的问题。 谢谢
from tkinter import *
from os import listdir
from os import path
from threading import Thread
def open():
print("open selected")
def save():
print("save selected")
def fontchange():
sheet.config(font = (str(selecfont.get())))
print(selecfont.get())
fonts = ["Arial","Courier New","Times New Roman","Verdana",
"System","Impact","Fixedsys","corbel"]
master = Tk()
master.config(bg = "#080d31")
master.title("Ed.co")
menubar = Menu(master,background = "#080d31")
filemenu = Menu(menubar,tearoff=0)
filemenu.add_command(label = "open",command = open)
filemenu.add_command(label = "save",command = save)
menubar.add_cascade(label = "file",menu = filemenu)
settingsmenu = Menu(menubar,tearoff=0)
fontmenu = Menu(settingsmenu,tearoff=0)
settingsmenu.add_cascade(label = "font",menu = fontmenu)
menubar.add_cascade(label = "settings",menu = settingsmenu)
for font in fonts:
selecfont = StringVar()
fontmenu.add_radiobutton(value = font,label = font,variable = selecfont,command = fontchange)
sheet = Text(master,width = 71,height = 15,font = "corbel")
sheet.grid(row=1,column=0,columnspan = 3)
master.config(menu = menubar)
master.mainloop()
错误信息:
sheet.config(font = (str(selecfont.get())))
File "C:\Python34\lib\tkinter\__init__.py", line 1319, in configure
return self._configure('configure', cnf, kw)
File "C:\Python34\lib\tkinter\__init__.py", line 1310, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: font "" doesn't exist
单选按钮需要共享一个公共变量,但您正在为每个单选按钮创建一个新变量。您需要将 selecFont
的定义移到循环之外:
selecFont = StringVar()
for font in fonts:
...