主tkinter模块和子模块其ttk在Python 3看起来包含相同窗口小部件(即Buttons,CheckButtons等).
因此,在创建按钮时,可以自由使用tkinter.Button小部件或tkinter.ttk.Button.
你知道他们之间有什么区别吗?你为什么选择其中一个?
这是给我带来麻烦的代码.
f = Frame(root, width=1000, bg="blue")
f.pack(fill=X, expand=True)
l = Label(f, text="hi", width=10, bg="red", fg="white")
l.pack()
Run Code Online (Sandbox Code Playgroud)
如果我使用Label注释掉行,则Frame会以正确的宽度显示.但是,添加Label似乎会将Frame缩小到Label的大小.有没有办法防止这种情况发生?
如何在构造函数中设置Tkinter Entry小部件的默认文本?我检查了文档,但是我没有看到类似于"string="在构造函数中设置的选项?
有一个类似的答案用于使用表和列表,但这是一个简单的Entry小部件.
我在类似的错误消息上看到了其他一些帖子,但找不到可以解决我的问题的解决方案.
我用TkInter稍微涉足并创建了一个非常简单的UI.该守则如下─
from tkinter import *
root = Tk()
def grabText(event):
print(entryBox.get())
entryBox = Entry(root, width=60).grid(row=2, column=1, sticky=W)
grabBtn = Button(root, text="Grab")
grabBtn.grid(row=8, column=1)
grabBtn.bind('<Button-1>', grabText)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
我启动并运行UI.当我单击Grab按钮时,我在控制台上收到以下错误:
C:\Python> python.exe myFiles\testBed.py
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python\lib\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args)
File "myFiles\testBed.py", line 10, in grabText
if entryBox.get().strip()=="":
AttributeError: 'NoneType' object has no attribute 'get'
Run Code Online (Sandbox Code Playgroud)
错误追溯到entryBox.
我敢肯定有人可能以前处理过这个问题.任何帮助表示赞赏.
如何在全屏模式下在Tkinter显示中制作画面?我看到了这段代码,它非常有用......:
>>> import Tkinter
>>> root = Tkinter.Tk()
>>> root.overrideredirect(True)
>>> root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
Run Code Online (Sandbox Code Playgroud)
...但是是否可以编辑代码以便命中 Esc 自动使窗口"恢复"?
我正在尝试创建一个文件选择器对话框.但是,当我尝试tkMessageBox在Python 3中导入时,我收到一个错误,声称该模块不存在.
import tkMessageBox
# ImportError: No module named 'tkMessageBox'
Run Code Online (Sandbox Code Playgroud)
尝试在Python 3中导入其他Tkinter模块时,我遇到类似的错误.
import Tkinter # No module named 'Tkinter'
import tkColorChooser # No module named 'tkColorChooser'
import tkFileDialog # No module named 'tkFileDialog'
Run Code Online (Sandbox Code Playgroud)
如何在Python 3中导入Tkinter模块?什么是新模块名称?
我正在第一次为Python3中的程序编写Browse按钮.我一直在搜索互联网和这个网站,甚至是python标准库.
我找到了示例代码和对事物的非常肤浅的解释,但是我找不到任何解决我直接遇到的问题的东西,或者说是一个足够好的解释,所以我可以根据自己的需要自定义代码.
这是相关的片段:
Button(self, text = "Browse", command = self.load_file, width = 10)\
.grid(row = 1, column = 0, sticky = W) .....
def load_file(self):
filename = filedialog.askopenfilename(filetypes = (("Template files", "*.tplate")
,("HTML files", "*.html;*.htm")
,("All files", "*.*") ))
if filename:
try:
self.settings["template"].set(filename)
except:
messagebox.showerror("Open Source File", "Failed to read file \n'%s'"%filename)
return
Run Code Online (Sandbox Code Playgroud)
该方法是我在自己的自定义过程中发现的一些代码的混合体.好像我终于让它工作了(有点),虽然它不是我需要它的方式.
当我激活"浏览"按钮时出现此错误:NameError: global name 'filedialog' is not defined.
我一路上发现了类似的问题,但我已经介绍了所有解决方案.我进入了IDLE的'filedialog'帮助部分,但也没有从那里收集任何东西.
有人会介意提供一个细分和一点指导; 我的书都没有专门解决它,我已经检查了提供给其他人的所有解决方案 - 我输了.
这不是一个问题,而是帮助可能遇到此问题的其他人的答案.
我想要想象一些基本颜色,所以我可以选择适合我的配色方案.我无法在任何地方找到颜色图表,因此修改了样本以显示它希望您发现它很有用.
import Tix as tk
# import tkinter.tix as tk # for Python 3
COLORS =['snow', 'ghost white', 'white smoke', 'gainsboro', 'floral white', 'old lace',
'linen', 'antique white', 'papaya whip', 'blanched almond', 'bisque', 'peach puff',
'navajo white', 'lemon chiffon', 'mint cream', 'azure', 'alice blue', 'lavender',
'lavender blush', 'misty rose', 'dark slate gray', 'dim gray', 'slate gray',
'light slate gray', 'gray', 'light grey', 'midnight blue', 'navy', 'cornflower blue', 'dark slate blue',
'slate blue', 'medium slate blue', 'light slate blue', 'medium …Run Code Online (Sandbox Code Playgroud) 我想在Ubuntu 14.04上运行python代码,但是当我执行它时,它会给我以下错误消息
Traceback (most recent call last):
File "main.py", line 2, in <module>
from tkinter import *
ImportError: No module named tkinter
Run Code Online (Sandbox Code Playgroud) python ×10
tkinter ×10
python-2.7 ×2
python-3.x ×2
filedialog ×1
frame ×1
label ×1
tix ×1
ttk ×1
ubuntu-14.04 ×1