我已编写此代码,试图在我的程序GUI
中添加CSVModify
和浏览按钮。
问题是,如果我在CSVModify()
函数下键入browseCSV
,则表示CSVModify
未定义。如果CSVModify
browseCSV
下的GUI
未按以下代码进行缩进,则我的CSVModify
不会显示,而是直接跳至browseCSV
功能。
我希望首先运行CSVModify
以便我可以选择一个文件,然后我想在此之后运行Python
来修改我选择的文件,该文件存储在变量filename下。我是#attempt to make CSVModifier with GUI
from Tkinter import *
import csv
class Window:
def __init__(self, master):
#File Selection
csvfile=Label(root, text="File").grid(row=1, column=0)
bar=Entry(master).grid(row=1, column=1)
#Buttons
y=7
self.button1 = Button(root, text="OK", command=master.destroy)
y+=1
self.button1.grid(row=10, column=3, sticky = W + E)
self.button2 = Button(root, text="Browse", command=self.browseCSV)
self.button2.grid(row=1, column=3)
def CSVModify():
new_columns = []
userinput = raw_input("What string would you like to remove? ")
changes = {
userinput : '',
}
with open(filename, 'rb') as f:
reader = csv.reader(f)
for column in reader:
new_column = column
for key, value in changes.items():
new_column = [x.replace(key, value) for x in new_column]
new_columns.append(new_column)
with open(("modified" + filename), 'wb') as f:
writer = csv.writer(f)
writer.writerows(new_columns)
def browseCSV(self):
from tkFileDialog import askopenfilename
filename = askopenfilename()
self.button3 = Button(root, text=filename,)
self.button3.grid()
CSVModify()
root = Tk()
window=Window(root)
root.mainloop()
的新手并且编码一般,所以请帮助我了解我哪里出错了。
button3
我对public int HwDecoderInputCallback( int size, long pts, int flag ){
int ret = 0, flags = 0;
final int TIME_OUT_US = 10000;
int inputBufIndex = 0;
try{
try{
Thread.sleep(5);
}catch(InterruptedException e){
}
inputBufIndex = chd.decoder.dequeueInputBuffer(TIME_OUT_US);
if( inputBufIndex >= 0 ){
ByteBuffer inputBuf = inputBuffer[inputBufIndex];
inputBuf.clear();
getVideoPacketHW(inputBuf, size);
chd.decoder.queueInputBuffer( inputBufIndex, 0, size, pts, flag);
}
}catch(Exception e1){
// Irrecoverable errors. Release Media codec and recreate resources.
videoWindow.glRenderer.restoreVideoSurface();
ret = -1;
}
return ret;
}
public int HwDecoderOutputCallBack(){
int status = 0;
final int TIME_OUT_US = 10000;
try{
int dec_status = chd.decoder.dequeueOutputBuffer(mBufferInfo, TIME_OUT_US);
if( dec_status == MediaCodec.INFO_TRY_AGAIN_LATER ){
Log.i("Candy MediaPlayer","Output try again");
}else if( dec_status == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED){
Log.i("Candy MediaPlayer","Output buffer changed");
outputBuffer = chd.decoder.getOutputBuffers();
}else if( dec_status == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED){
Log.i("Candy MediaPlayer","Buffer fmt changed");
}else if( dec_status < 0){
Log.i("Candy MediaPlayer","Output buffer index < 0");
}else{
if(((mBufferInfo.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM)!=0)){
Log.i("Candy MediaPlayer","End of stream.");
chd.decoder.releaseOutputBuffer(dec_status, false);
status = 1;
}else{
candySyncVideoHW( mBufferInfo.presentationTimeUs );
chd.decoder.releaseOutputBuffer(dec_status, true);
status = 0;
}
}
}
catch( Exception e){
// Irrecoverable errors. Release Media codec and recreate resources.
videoWindow.glRenderer.restoreVideoSurface();
}
return status;
}
的代码只是为了测试文件路径是否正确找到并存储。
答案 0 :(得分:0)
您的代码中至少有一个问题是您要创建两个根窗口(root = Tk()
和Tk().withdraw()
)。 Tkinter设计为只有一个根窗口运行。
您可能还有其他问题,但代码中的缩进使得无法知道您的代码实际上是什么样的。
答案 1 :(得分:0)
解决了这个问题。感谢所有评论过有用解决方案等的人。它最终成为我完全错误地定义和调用CSVModify函数的事实。以下是感兴趣的任何人的更新代码。
#attempt to make CSVModifier with GUI
from Tkinter import *
import csv
class Window:
def __init__(self, master):
#File Selection
csvfile=Label(root, text="File").grid(row=1, column=0)
bar=Entry(master).grid(row=1, column=1)
#Buttons
y=7
self.button1 = Button(root, text="OK", command=master.destroy)
y+=1
self.button1.grid(row=10, column=3, sticky = W + E)
self.button2 = Button(root, text="Browse", command=self.browseCSV)
self.button2.grid(row=1, column=3)
def browseCSV(self):
from tkFileDialog import askopenfilename
filename = askopenfilename()
self.button3 = Button(root, text=filename)
self.button3.grid()
def CSVModify():
new_columns = []
userinput = raw_input("What string would you like to remove? ")
changes = {
userinput : '',
}
with open(filename, 'rb') as f:
reader = csv.reader(f)
for column in reader:
new_column = column
for key, value in changes.items():
new_column = [x.replace(key, value) for x in new_column]
new_columns.append(new_column)
with open(("modified" + filename), 'wb') as f:
writer = csv.writer(f)
writer.writerows(new_columns)
root = Tk()
window=Window(root)
root.mainloop()
CSVModify()