我正在尝试根据另一个组合框的选择来填充组合框。如果我使用更新按钮来更新组合框#1 选择,我就可以做到这一点。但是,我想动态进行更新。
我附上了一个简单的代码示例,除了一个问题之外,该代码似乎按预期工作。您必须选择组合框 #1 两次才能更新组合框 #2。如果有人对两个如何解决这个问题有任何想法,我们将不胜感激。因为我是通过示例学习的,所以请您提供一个代码示例。
预先感谢您:begez
from tkinter import *
import tkinter.ttk,csv
global CategoryCombo
def getUpdateData():
cat = CategoryCombo.get()
if cat == 'car':
AccountCombo = tkinter.ttk.Combobox( width = 15,value = car)
AccountCombo.grid(row = 5,column = 1,pady = 25,sticky = E)
elif cat == 'home':
AccountCombo = tkinter.ttk.Combobox( width = 15,value = home)
AccountCombo.grid(row = 5,column = 1,pady = 25,sticky = E)
elif cat == 'rv':
AccountCombo = tkinter.ttk.Combobox( width = 15,value = rv)
AccountCombo.grid(row = 5,column = 1,pady = …Run Code Online (Sandbox Code Playgroud) 我的 Tkinter GUI 中有 20 个使用 for 循环创建的条目(将来可能会有更多条目,我真的不希望有 50 行代码只是为了定义条目)。我需要收集条目值以创建一个 numpy 数组。作为在黑暗中的一枪,我尝试过这个:
master = Tk()
R=StringVar()
namR = []
for ii in range(0,20):
namR.append(Entry(master), textvariable=R[ii])
namR[ii].grid(row=2+ii, column=3)
Run Code Online (Sandbox Code Playgroud)
这显然行不通(StringVar instance has no attribute '__getitem__'),但我认为目标很明确。
请问有什么建议可以让这项工作成功吗?
我正在编写一个计时器应用程序,它遇到了很多困难,但第一个困难是制作一个可以通过按按钮打破的循环。我研究过它,我的研究表明我应该使用线程,但我不知道它是如何工作的。
然后,我决定尝试的是在调用键盘中断时例外,然后创建一个调用同一中断的按钮。但是,当我按 ctrl-c 时,我当前的代码拒绝中断。
我的示例代码如下所示
from Tkinter import *
from sys import exit
class Timer:
def __init__(self, master):
buttonstart = Button(master, text = "Start", fg = "blue", command = self.start)
buttonstart.grid(row = 1, column = 0)
buttonquit = Button(master, text = "Quit", fg = "blue", command= quit)
buttonquit.grid(row = 1, column = 2)
global timertext
timertext = DoubleVar()
timertext.set(0)
display = Label(master, textvariable = timertext)
display.grid(row = 0, column = 0)
timertext.set(timertext)
def timerlogic(self):
pass
def pause(self):
pass
def start(self): …Run Code Online (Sandbox Code Playgroud) 我已经确定,在 Python TkInter GUI 程序中,最佳实践是将整个内容包含在 try / except 块中,以便捕获所有异常并将其呈现给最终用户(而不是默默地出现问题或程序似乎无缘无故地退出)。
然而,这种方法存在一些问题。考虑以下小程序,当单击按钮时,该程序会尝试除以 0:
import tkinter
class Foo():
def __init__(self):
# Initialize a new GUI window
root = tkinter.Tk()
# The "Generic error" message is shown when the following is uncommented
#number = 1 / 0
# Define a button and draw it
button = tkinter.Button(root, text='Generate an error', command=self.generate_error)
button.pack()
# Loop forever
root.mainloop()
def generate_error(self):
# The "Generic error" message is not shown
number = 1 / 0
if __name__ == …Run Code Online (Sandbox Code Playgroud) 我想在我的 tkinter 应用程序中运行一些额外的窗口(tkinter 8.6、python 3.5、ubuntu xenial)。虽然我可以从主应用程序中选择删除窗口协议,但它永远不会在其他窗口上被调用。我想让用户用“X”关闭窗口,但我确实需要知道他们何时这样做。
我希望一旦我看到答案,答案就会非常明显!
这个简单的测试应用程序演示了......
#!/usr/bin/python3
import tkinter
class app(tkinter.Tk):
def __init__(self):
super().__init__()
self.child1 = None
self.geometry('600x400')
w = tkinter.Button(self, command=self.bclick, text='button1')
w.pack()
self.protocol("WM_DELETE_WINDOW", self.appClose)
def bclick(self):
if self.child1 == None:
self.child1 = tkinter.Toplevel()
else:
self.child1.destroy()
self.child1 = None
def appClose(self):
print('main app close')
self.destroy()
class wind1(tkinter.Toplevel):
def __init__(self):
super().__init__()
self.title('window 1')
self.protocol("WM_DELETE_WINDOW", self.window1Close)
self.geometry('600x400')
def window1Close(self):
print("window1Close")
self.destroy()
if __name__=="__main__":
print(tkinter.TkVersion)
app().mainloop()
Run Code Online (Sandbox Code Playgroud) 这可能是一个简单的答案。
我有一个文本小部件,我想将任何字母数字键盘键(a-zA-Z0-9_ 和其余常规键)绑定到特定方法,并将Ctrl+F绑定到另一个方法。
使用widget.bind("<Key>", method)将创建以下内容:
method两次,一次用于Ctrl( event.keysym = Control_L, event.char = None),第二次用于F( event.keysym = f, event.char = <invalid>)method( event.keysym = f, event.char = f)有没有办法区分这两种情况?
使用 TK GUI 的 Python 应用程序通常调用以下导入语句:
from tkinter import *
from tkinter import ttk
Run Code Online (Sandbox Code Playgroud)
打电话不是from tkinter import ttk多余的吗?我以为 ttk 已经通过调用导入from tkinter import *
请解释为什么ttk需要单独导入?
(我已经知道它有“改进的小部件”。我想了解为什么无法从调用中访问改进的小部件from tkinter import *。)
我有一个程序可以根据窗口的大小更改 tkinter 画布上对象的位置。这是可行的,但它会在每个循环中运行,并且当存在大量对象时会显着减慢程序速度,因为它会删除并重新绘制它们。
有没有一种方法可以检查窗口大小的变化,以便在需要时只需运行一次?
我的窗口中正在运行一个动画,每当用户拖动窗口时我想暂停该动画,以确保交互顺利。
我已经尝试过以下方法:
root.bind("<ButtonPress-1>", lambda e: start_stop_animation(False))
root.bind("<B1-Motion>", lambda e: start_stop_animation(False))
root.bind("<ButtonRelease-1>", lambda e: start_stop_animation(self._is_running))
Run Code Online (Sandbox Code Playgroud)
看来这些调用根本不绑定到标题栏。
我想在不删除标题栏的情况下使用root.overrideredirect(True),除非有一种简单的方法将其替换为能够捕获这些事件的类似标题栏。
self.mrEntry.event_delete(\'<<Paste>>\', \'<Control-v>\')\nself.mrEntry.event_add(\'<Control-v>\', lambda *_: self.handle_clipboard()) # ERROR occurs\n\ndef handle_clipboard(self):\n # Split the clipboard text up by every \'\\n\' and distribute them among the entries\n # In contrast to pasting all the text into one entry\nRun Code Online (Sandbox Code Playgroud)\n\n是否可以覆盖Control-v快捷方式,将剪贴板分布在多个条目中,而不是将所有内容粘贴到一个条目中?
从聚焦的条目开始,对于\\n剪贴板中的每个条目,将其粘贴到后续条目中。
负责剪贴板处理的函数:(它粘贴文本两次,因为我们有一个要粘贴的全局绑定,以及一个对某些选定条目的显式绑定。)
\n\ndef handle_clipboard(self, focused_entry):\n """Function to destribute the clipboard data into seperate entries"""\n\n if \'\\n\' not in root.clipboard_get():\n # If there is only one line in clipboard, paste it all in the focused cell\n return\n\n …Run Code Online (Sandbox Code Playgroud) tkinter ×10
python ×9
python-2.7 ×2
python-3.x ×2
combobox ×1
linux ×1
loops ×1
numpy ×1
tk-toolkit ×1
windows ×1