2015-10-16 69 views
0

我已经用Python编写了一个程序,允许我一次更改多个文件的名称。我有一个很奇怪的问题。raw_input停止GUI出现

当我使用raw_input来获得我想要的扩展名时,GUI将不会启动。我没有得到任何错误,但窗口永远不会出现。

我尝试使用raw_input作为从用户获取文件扩展名的方式来构建文件列表。当raw_input未被使用时,这个程序将正常工作。我指的代码段在我的globList函数中。出于某种原因,当使用raw_imput时,窗口将不会启动。

import os 
import Tkinter 
import glob 
from Tkinter import * 

def changeNames(dynamic_entry_list, filelist): 
    for index in range(len(dynamic_entry_list)): 
     if(dynamic_entry_list[index].get() != filelist[index]): 
      os.rename(filelist[index], dynamic_entry_list[index].get()) 

    print "The files have been updated!" 



def drawWindow(filelist): 

    dynamic_entry_list = [] 
    my_row = 0 
    my_column = 0 
    for name in filelist: 
     my_column = 0 
     label = Tkinter.Label(window, text = name, justify = RIGHT) 
     label.grid(row = my_row, column = my_column) 

     my_column = 1 
     entry = Entry(window, width = 50) 
     dynamic_entry_list.append(entry) 
     entry.insert(0, name) 
     entry.grid(row = my_row, column = my_column) 

     my_row += 1  
    return dynamic_entry_list 


def globList(filelist): 
    #ext = raw_input("Enter the file extension:") 
    ext = "" 
    desired = '*' + ext 
    for name in glob.glob(desired): 
     filelist.append(name) 




filelist = [] 
globList(filelist) 
window = Tkinter.Tk() 
user_input = drawWindow(filelist) 
button = Button(window, text = "Change File Names", command = (lambda  e=user_input: changeNames(e, filelist))) 
button.grid(row = len(filelist) + 1 , column = 1) 

window.mainloop() 

这是raw_input的问题吗?

什么是解决问题的好方法?

+0

您使用的是IDE:

在弹出对话框一个体面的教程可以在effbot网站上找到? – Kevin

回答

0

这是如何定义tkinter的工作。它是单线程的,所以在等待用户输入时,它确实在等待。 mainloop必须正在运行,以便GUI可以响应事件,包括内部事件,例如请求在屏幕上绘制窗口。

一般来说,您不应该将图形用户界面与标准输入读取输入混合在一起。如果您正在创建GUI,请通过输入窗口小部件获取用户的输入。或者,在创建GUI之前获取用户输入。 http://effbot.org/tkinterbook/tkinter-dialog-windows.htm