我有 ttk.Notebook 类的笔记本小部件。在这个笔记本中,我添加了许多标签:
notebook.add(child=my_object, text='my tab')
如何获取选项卡对象而不是选项卡名称?
所以,有儿童词典。好的,带来下一个问题。
我真正想做的是在 Notebook 中操作活动选项卡的对象。所以我有这个:
notebook.children[notebook.select().split('.')[2]])
Run Code Online (Sandbox Code Playgroud)
但它看起来很丑。不幸的是, select() 返回的小部件名称与 children[] 中的小部件名称格式不完全相同。在这种情况下,是否有任何方法可以将一种格式可靠地转换为另一种格式,或者 split() 永远不会失败?
这是问题的演示:
#!/usr/bin/env python3
from tkinter import *
from tkinter.ttk import *
class App(Tk):
def __init__(self):
super().__init__()
notebook = Notebook(self)
notebook.add(child=Frame(notebook), text='tab1')
notebook.add(child=Frame(notebook), text='tab2')
notebook.add(child=Frame(notebook), text='tab3')
notebook.pack()
print('Tabs are: ', notebook.children.keys())
print('Active tab: ', notebook.select())
print(type(notebook.children[notebook.select().split('.')[2]]))
App().mainloop()
Run Code Online (Sandbox Code Playgroud) def lock():
global pas,lk
lk=Tk.Frame(main)
lk.pack(fill="both", anchor="center", expand=1)
Tk.Label(lk, text="", font="arial 75").pack(anchor="center", side="top")
fpas=Tk.LabelFrame(lk, text="Zadejte heslo:")
fpas.pack(anchor="center")
pas=Tk.Entry(fpas)
pas.pack()
Tk.Button(lk, text="OK", command=check).pack(side="top")
Tk.Button(lk, text="Vypnout", command=lkend).pack(side="top")
return
Run Code Online (Sandbox Code Playgroud)
This is a part of my code and I want to change it. When I write my password into Tk.Entry called pas, I don´t want to see numbers, but a only specific symbol, such as * or another symbol. Thanks for the answer.
P.S: Please don´t evaluate my code, I know that I …
我正在尝试学习 tkinter,我的想法要求它全屏显示。然而,在使其全屏显示之前,我想确保可以使用转义关闭窗口。因此,通过 Stack Overflow 上与此类似的其他问题,我一直试图让它在我按转义键时销毁 tkinter 窗口。对我来说,这似乎应该有效,但当我点击转义时,我遇到了异常:
`Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Jake\AppData\Local\Programs\Python\Python36-
32\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
TypeError: destroy() takes 1 positional argument but 2 were given`
Run Code Online (Sandbox Code Playgroud)
这让我很困惑,因为我认为我根本没有调用任何参数,更不用说2了。我添加了一个退出按钮,它调用close我创建的方法并且有效,但使用转义似乎不起作用。为了清楚起见,我提供了我的代码。我知道这与这里的很多问题类似,但我已经尝试了一切,但似乎没有什么对我有用。提前致谢!
import tkinter
from tkinter import *
class Window(Frame):
def __init__(self, master = None):
Frame.__init__(self, master)
master.bind('<Escape>', master.destroy)
self.init_window()
def init_window(self):
self.pack(fill=BOTH, expand=1)
quitButton = Button(self, text="quit", command=self.close)
quitButton.place(x=0, y=0)
def close(self):
self.master.destroy()
def main():
root = Tk()
root.geometry('500x500')
app = Window(root)
root.mainloop() …Run Code Online (Sandbox Code Playgroud) 我定义了一个简单的OptionMenu像
import Tkinter as tk
optionList = ('a', 'b', 'c')
v = tk.StringVar()
v.set(optionList[0])
om = tk.OptionMenu(self, v, *optionList)
Run Code Online (Sandbox Code Playgroud)
此列表将显示a为默认值,这很好。但是也定义了命令按钮,最终需要对其进行更改以显示另一个可用选项(例如b)。如何做到这一点?
基本上我想在屏幕的一侧放置一个时钟,在另一侧放置文本,我使用框架。我怎样才能做到这一点。这是它现在的样子的图片:
我想让时钟与同一行上的文本一致,但有不同的标签。看看我的代码,看看你是否能以某种方式帮助我,拜托!
from tkinter import *
from tkinter import ttk
import time
root = Tk()
root.state("zoomed") #to make it full screen
root.title("Vehicle Window Fitting - Management System")
root.configure(bg="grey80")
Title = Frame(root, width=675, height=50, bd=4, relief="ridge")
Title.pack(side=TOP, anchor='w')
titleLabel = Label(Title, font=('arial', 12, 'bold'), text="Vehicle Window Fitting - Management System", bd=5, anchor='w')
titleLabel.grid(row=0, column=0)
clockFrame = Frame(root, width=675, height=50, bd=4, relief="ridge")
clockFrame.pack(side=TOP, anchor='e')
clockLabel = Label(clockFrame, font=('arial', 12, 'bold'), bd=5, anchor='e')
clockLabel.grid(row=0, column=1)
curtime = ""
def tick():
global curtime
newtime …Run Code Online (Sandbox Code Playgroud) from tkinter import *
root = Tk()
def changeCursor(event, pointerName):
root.cursor(pointerName)
link = Label(root, text="Link")
link.bind("<Motion>", lambda event : changeCursor(event, "hand"))
link.pack()
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
当我的光标悬停在它上面时,我希望我的光标变成一只“手”。当光标离开标签占据的区域时,我还想将光标改回箭头。但是我收到以下错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Arnob\AppData\Local\Programs\Python\Python36-
32\lib\idlelib\run.py", line 137, in main
seq, request = rpc.request_queue.get(block=True, timeout=0.05)
File "C:\Users\Arnob\AppData\Local\Programs\Python\Python36-
32\lib\queue.py", line 172, in get
raise Empty
queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Arnob\AppData\Local\Programs\Python\Python36-
32\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "<pyshell#7>", …Run Code Online (Sandbox Code Playgroud) 我遇到了如何将我的sqlite3数据库中的所有记录输出到tkinter treeview.It 中的问题。它只输出中的最后一条记录,db并且记录也没有出现在为其指定的列中。该print方法将所有db记录打印到我的终端,但不输出treeview小部件的所有记录。
欢迎您提出建议以实现这一目标
from tkinter import ttk
import tkinter as tk
import sqlite3
def connect():
conn = sqlite3.connect("TRIAL.db")
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS profile(id INTEGER PRIMARY KEY,
First TEXT, Surname TEXT)")
conn.commit()
conn.close()
def View():
conn = sqlite3.connect("TRIAL.db")
cur = conn.cursor()
cur.execute("SELECT * FROM profile")
rows = cur.fetchall()
for row in rows:
print(row) # it print all records in the database
tree.insert("", tk.END, values=row) …Run Code Online (Sandbox Code Playgroud) 我正在使用 Tkinter 在 Python 3.6 中编写程序,其中客户有多个(11)个输入字段。我希望这些输入字段只接受整数,并且还能够定义最大字符数。
我已经有一个功能可以做到这一点。但此功能仅适用于一个输入字段。我尝试通过调用函数来输入变量,因此它会更改另一个输入字段。我无法做到这一点。
这是我的功能,适用于 1 个输入字段。
def limitMuntgeld(self, *args):
value = self.invoerM.get()
if len(value) > 5:
self.invoerM.set(value[:5])
if value.lower() in "abcdefghijklmnopqrstuvwxyz-=[];/":
self.invoerM.set(value[:0])
Run Code Online (Sandbox Code Playgroud)
这是与函数一起使用的示例输入字段代码
self.invoerMuntgeld = Entry(self, font=('Arial', 14), textvariable=self.invoerM)
Run Code Online (Sandbox Code Playgroud)
这与下面发布的输入字段上的跟踪相结合。
self.invoerM = StringVar()
self.invoerM.trace('w', self.limitMuntgeld)
Run Code Online (Sandbox Code Playgroud)
我也用 vcmd 和 validatecommand 尝试过。然而,没有好的结果。我的最终结果将是一个处理所有输入字段的函数。如果有人有任何建议,我会全神贯注!
我在 stackover 和许多其他网站上尝试了所有解决方案。但它仍然给出同样的错误。我什至无法安装它。
C:\Users\***\Google Drive\Python>pip install tkinter
Collecting tkinter
Could not find a version that satisfies the requirement tkinter (from versions: )
No matching distribution found for tkinter
C:\Users\***\Google Drive\Python>pip install Tkinter
Collecting Tkinter
Could not find a version that satisfies the requirement Tkinter (from versions: )
No matching distribution found for Tkinter
C:\Users\***\Google Drive\Python>pip install Tk
Collecting Tk
Could not find a version that satisfies the requirement Tk (from versions: )
No matching distribution found for Tk
C:\Users\***\Google Drive\Python>pip …Run Code Online (Sandbox Code Playgroud) tkinter ×10
python ×9
python-3.x ×3
events ×1
optionmenu ×1
python-2.7 ×1
sqlite ×1
tabs ×1
tk-toolkit ×1
treeview ×1