相关疑难解决方法(0)

在Python 3中重命名了哪些tkinter模块?

我正在尝试创建一个文件选择器对话框.但是,当我尝试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模块?什么是新模块名称?

python tkinter python-3.x

37
推荐指数
1
解决办法
4万
查看次数

覆盖Tkinter"X"按钮控件(关闭窗口的按钮)

当用户按下我创建的关闭时 Button,某些任务在退出之前执行.但是,如果用户单击[X]窗口右上角的按钮关闭窗口,则无法执行这些任务.

如何覆盖用户单击[X]按钮时发生的情况?

python tkinter button

26
推荐指数
2
解决办法
4万
查看次数

拦截Tkinter"退出"命令?

我正在用Tkinter用Python编写客户端服务器程序.我需要服务器来跟踪连接的客户端.为此,我希望客户端在单击退出按钮(角落中的标准"X")后向服务器发送自动消息.我怎么知道用户何时退出该程序?

python client-server tkinter exit

6
推荐指数
2
解决办法
2万
查看次数

关闭 tkinter 应用程序时,Python 线程调用不会完成

我正在 python 中使用 tkinter 制作一个计时器。该小部件只有一个按钮。该按钮兼作显示剩余时间的元素。计时器有一个线程,可以简单地更新按钮上显示的时间。

该线程仅使用一个 while 循环,该循环应在设置事件时停止。当窗口关闭时,我使用协议调用设置此事件的函数,然后尝试加入线程。这在大多数情况下都有效。但是,如果我在进行某个调用时关闭程序,则会失败并且线程在窗口关闭后继续运行。

我知道有关关闭 tkinter 窗口时关闭线程的其他 类似线程。但这些答案已经过时了,如果可能的话,我想避免使用 thread.stop() 。

我尝试尽可能减少这一点,同时仍然表明我对该计划的意图。

import tkinter as tk
from tkinter import TclError, ttk
from datetime import timedelta
import time
import threading
from threading import Event

def strfdelta(tdelta):
    # Includes microseconds
    hours, rem = divmod(tdelta.seconds, 3600)
    minutes, seconds = divmod(rem, 60)
    return str(hours).rjust(2, '0') + ":" + str(minutes).rjust(2, '0') + \
           ":" + str(seconds).rjust(2, '0') + ":" + str(tdelta.microseconds).rjust(6, '0')[0:2]

class App(tk.Tk): …
Run Code Online (Sandbox Code Playgroud)

python multithreading tkinter python-3.x

5
推荐指数
1
解决办法
550
查看次数

如何检测python Turtle图形中的X(关闭)按钮?

当我在 Turtle 图形中运行无限循环绘图时单击 X(关闭)按钮时,会出现一些错误消息。

下面是一个例子:

import turtle

wn = turtle.Screen()
tess = turtle.Turtle()

while True:
    tess.forward(50)
    tess.left(120)
    tess.forward(50)

wn.mainloop()
Run Code Online (Sandbox Code Playgroud)

当我关闭窗口时,会出现以下错误消息。

Traceback (most recent call last):
  File "/Users/user/Downloads/test.py", line 8, in <module>
    tess.forward(50)
  File "/Users/user/anaconda3/lib/python3.6/turtle.py", line 1637, in forward
    self._go(distance)
  File "/Users/user/anaconda3/lib/python3.6/turtle.py", line 1605, in _go
    self._goto(ende)
  File "/Users/user/anaconda3/lib/python3.6/turtle.py", line 3178, in _goto
    self._pencolor, self._pensize, top)
  File "/Users/user/anaconda3/lib/python3.6/turtle.py", line 545, in _drawline
    self.cv.coords(lineitem, *cl)
  File "<string>", line 1, in coords
  File "/Users/user/anaconda3/lib/python3.6/tkinter/__init__.py", line 2463, in coords
    self.tk.call((self._w, 'coords') + args))]
_tkinter.TclError: …
Run Code Online (Sandbox Code Playgroud)

python turtle-graphics

3
推荐指数
1
解决办法
1405
查看次数