我正在开发一个简单的消息传递系统,需要将以下内容添加到Tkinter文本小部件:
我知道tkinter Text小部件能够通过标记机制使用多种字体和颜色,但我不明白如何使用这些功能.
如何使用Text小部件的功能实现这些功能?具体来说,我如何更改单词的字体系列,颜色和大小,以及如何使用它来实现拼写检查,其中拼写错误的单词加下划线或着色与文本的其余部分不同.
Python 3中的tkFileDialog模块在哪里?使用简单的Dialog在Python中选择文件的问题引用模块使用:
from Tkinter import Tk
from tkFileDialog import askopenfilename
Run Code Online (Sandbox Code Playgroud)
但在Python 3中使用它(在将Tkinter更改为tkinter之后)获得:
Traceback (most recent call last):
File "C:\Documents and Settings\me\My Documents\file.pyw", line 5, in <module>
import tkFileDialog
ImportError: No module named tkFileDialog
Run Code Online (Sandbox Code Playgroud)
python 2.7.2 doc(docs.python.org)说:
tkFileDialog
Common dialogs to allow the user to specify a file to open or save.
These have been renamed as well in Python 3.0; they were all made submodules of the new tkinter package.
Run Code Online (Sandbox Code Playgroud)
但它没有提示新名称是什么,在3.2.2文档中搜索tkFileDialog和askopenfilename根本不返回任何内容(甚至没有从旧名称到新子模块名称的映射.)
尝试显而易见的不做杰克:
from tkinter import askopenfilename, asksaveasfilename
ImportError: …Run Code Online (Sandbox Code Playgroud) 我有以下代码
window = Tk()
window.lift()
window.attributes("-topmost", True)
Run Code Online (Sandbox Code Playgroud)
这段代码的工作原理是它在所有其他窗口上方显示我的Tkinter窗口,但它仍然只解决了一半的问题.虽然窗口实际上显示在所有其他窗口之上,但窗口没有焦点.有没有办法不仅让窗口成为Tkinter的最前面的窗口,而且还把重点放在它上面?
我有一个Checkbutton和一个IntVar与之关联的对象,但是当我试图得到它的值时var,我收到了PY_VAR0.
这是我的代码:
from tkinter import *
root = Tk()
def show_state():
print(var)
var = IntVar()
cbtn = Checkbutton(root, text='Check', variable=var, command=show_state)
cbtn.pack()
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
我为什么要来PY_VAR0?
我听说Python中的线程不容易处理,而且它们与tkinter变得更加混乱.
我有以下问题.我有两个类,一个用于GUI,另一个用于无限过程.首先,我启动GUI类,然后是无限进程的类.我希望当你关闭GUI时,它也完成了无限的过程,程序结束了.
代码的简化版本如下:
import time, threading
from tkinter import *
from tkinter import messagebox
finish = False
class tkinterGUI(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global finish
#Main Window
self.mainWindow = Tk()
self.mainWindow.geometry("200x200")
self.mainWindow.title("My GUI Title")
#Label
lbCommand = Label(self.mainWindow, text="Hello world", font=("Courier New", 16)).place(x=20, y=20)
#Start
self.mainWindow.mainloop()
#When the GUI is closed we set finish to "True"
finish = True
class InfiniteProcess(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global finish
while not finish:
print("Infinite Loop")
time.sleep(3)
GUI = tkinterGUI()
GUI.start()
Process …Run Code Online (Sandbox Code Playgroud) 我在tkinter中创建了一个登录窗口,它有两个Entry字段,第一个是Username,第二个是Password.
码
from tkinter import *
ui = Tk()
e1 = Entry(ui)
#i need a placeholder "Username" in the above entry field
e1.pack()
ui.mainloop()
Run Code Online (Sandbox Code Playgroud)
我想要一个名为"用户名"的占位符Entry,但如果你点击里面,文字应该消失.
我得到了这个Text小部件,我希望它能够使用网格几何管理器扩展并填充整个父节点.
根据我见过的例子,这个示例程序应该可以工作,唉它没有,当扩展窗口时,内容没有调整大小.
from Tkinter import *
root = Tk()
input_text_area = Text(root)
input_text_area.grid(row=1, column=0, columnspan=4, sticky=W+E)
input_text_area.configure(background='#4D4D4D')
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏
值得一提的是,我在Python 2.7(最新的2.x版本)中运行,并在PyCharm中进行编码,尽管我不认为IDE是相关的.
我试图弄清楚如何更改Tkinter框架的标题.下面是模拟我尝试更改标题的程序部分的简化代码:
from Tkinter import *
class start_window(Frame):
def __init__(self, parent=None):
Frame.__init__(self, parent)
Frame.pack(self)
Label(self, text = 'Test', width=30).pack()
if __name__ == '__main__':
start_window().mainloop()
Run Code Online (Sandbox Code Playgroud)
使用此示例代码,Frame具有标准的"tk"标题,但我想将其更改为"我的数据库".我已经尝试了所有我能想到的但没有成功的事情.任何帮助,将不胜感激.
一个.已将小部件放在网格的第0行中,如下所示.
self.a_button = Button(root, text="A Button")
self.a_button.grid(row=0, column=1)
Run Code Online (Sandbox Code Playgroud)
湾 并尝试在网格内的第2行放置另一个小部件.
self.b_button = Button(root, text="B Button")
self.b_button.grid(row=2, column=1)
Run Code Online (Sandbox Code Playgroud)
但是当我运行程序时,我看不到小部件之间有任何空间,而是一个接一个地堆叠.
那么,我如何编程以允许放置在不同行中的两个小部件之间的空间?分享您的意见!!
我正在使用Amazon Linux ec2机器.当我尝试在virtualenv中运行Python脚本时,我收到以下消息:
File "/home/sp/Envs/crispor/local/lib/python2.7/dist-packages/matplotlib/externals/six.py", line 80, in _import_module
__import__(name)
ImportError: No module named Tkinter
Run Code Online (Sandbox Code Playgroud)
据我所知,Tkinter应该是Python安装的一部分.但不知何故,事实并非如此.这些不起作用 -
sudo yum install python-tk
sudo yum install tkinter
Run Code Online (Sandbox Code Playgroud)
如何安装Tkinter?或者我应该这样做,它应该是Python安装的一部分?
python ×10
tkinter ×10
python-3.x ×3
amazon-ec2 ×1
checkbox ×1
focus ×1
frame ×1
linux ×1
message ×1
python-2.7 ×1
tcl ×1
text ×1
title ×1
tk-toolkit ×1
windows ×1