我正在制作文本编辑程序,但是当我运行此代码时 它不起作用,因为在第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
答案 0 :(得分:2)
Radiobuttons需要共享一个公共变量,但您要为每个单选按钮创建一个新变量。您需要在循环之外移动selecFont
的定义:
selecFont = StringVar()
for font in fonts:
...