简体   繁体   English

Python 2.X / Tkinter需要打印才能运行

[英]Python 2.X / Tkinter needs print to run

Firstly, very much Py Newby! 首先,非常Py Newby! I have written a program to import data from a file and display it as an image using tkinter. 我编写了一个程序来从文件导入数据,并使用tkinter将其显示为图像。 The loop that is misbehaving runs thus: 行为异常的循环因此运行:

Get data and plot 获取数据并绘制

for x in xrange(WIDE):
    for y in xrange(HIGH):
        dataPointLo = inFile.read(1)
        dataPointHi = inFile.read(1)
        pixelValue = ((ord(dataPointLo) + 256*(ord(dataPointHi)))-31500)
        colour = rgb[pixelValue]
        #print below makes prog run!
        print pixelValue
        img.put(colour, to=(x,y))

As suggested by the comment, leaving out the print stops it working, but it locks one core of the processor at 100% for as long as you leave it (well at least 20 mins!). 正如评论所建议的那样,将打印留空会使其停止工作,但是只要您离开处理器,它就会将处理器的一个核心锁定在100%的状态(至少20分钟!)。 This effect occurs both in IDLE and from the command line (Ubuntu 12.04). 在IDLE和命令行中都会发生这种效果(Ubuntu 12.04)。 Of course, the print to the IDLE window slows the program down, so I would like to remove it! 当然,在“ IDLE”窗口中打印会降低程序速度,因此我想将其删除! Any thoughts? 有什么想法吗?

it sounds like the process you are running takes a long time to complete, i would suggest that the reason you think it stops is because the window doesn't update while the process is busy unless you tell it to. 听起来您正在运行的进程需要很长时间才能完成,我建议您认为它停止的原因是,除非您告知,否则在进程繁忙时窗口不会更新。 i suggest you add a function like the following to your code and call it once before you enter your loop: 我建议您在代码中添加以下函数,并在进入循环之前调用一次:

def keep_alive(self):
    self.update()
    self.after(100, self.keep_alive)

this way you are adding an event to update the window every 100ms(ish) to the event loop, which will keep the program responsive. 这样,您将向事件循环添加一个事件,以每100ms(ish)更新一次窗口,这将使程序保持响应。 you can adjust the timing to suit you, too often will slow your loop down, too far apart and the program will feel sluggish. 您可以根据自己的需要调整时间,但时常会减慢循环速度,相距太远并且程序会变慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM