获取运行askopenfilename的按钮的结果

时间:2019-06-26 14:35:07

标签: python button tkinter

我有一个运行功能 // Also maintains the per-device operation queue. private class GattHandler extends BluetoothGattCallback { // Local copy of the key to BLE.mGatt. Fed by BLE.mNextGattHandle. final int mHandle; // The queue of operations. LinkedList<Runnable> mOperations = new LinkedList<Runnable>(); // connect() and rssi() are handled separately from other operations. CallbackContext mConnectContext; CallbackContext mRssiContext; CallbackContext mCurrentOpContext; // Flag used when writing notification config descriptor. // In this case we don't want to send back the result to JavaScript. boolean mDontReportWriteDescriptor = false; // The Android API connection. BluetoothGatt mGatt; // Maps of integer to Gatt subobject. HashMap<Integer, BluetoothGattService> mServices; HashMap<Integer, BluetoothGattCharacteristic> mCharacteristics; HashMap<Integer, BluetoothGattDescriptor> mDescriptors; // Monotonically incrementing key to the subobject maps. int mNextHandle = 1; // Notification callbacks. The BluetoothGattCharacteristic object, as found // in the mCharacteristics map, is the key. HashMap<BluetoothGattCharacteristic, CallbackContext> mNotifications = new HashMap<BluetoothGattCharacteristic, CallbackContext>(); GattHandler(int h, CallbackContext cc) { mHandle = h; mConnectContext = cc; } // Run the next operation, if any. // TODO: Make another method processNext that sets mCurrentOpContext to // null and calls process. That would clean up repeated code a bit. // Also consider writing method that adds a runnable to the mOperations // queue and calls process, this would also reduce some repeated code. void process() { if (mCurrentOpContext != null) return; Runnable runnable = mOperations.poll(); if (runnable == null) return; runAction(runnable); } @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { Log.i("@@@@@@", "@@@ onConnectionStateChange status: " + status + " newState: " + newState); if (status == BluetoothGatt.GATT_SUCCESS) { try { JSONObject result = new JSONObject(); result.put("deviceHandle", mHandle); result.put("state", newState); Log.i("@@@@@@", "@@@ connect success"); keepCallback(mConnectContext, result); } catch(JSONException e) { Log.i("@@@@@@", "@@@ connect error: " + e); e.printStackTrace(); mConnectContext.error("Connect error: " + e); //assert(false); } } else { // Could this be where we get 133? Yes it is. Log.i("@@@@@@", "@@@ connect error - status: " + status); mConnectContext.error(status); } } 的按钮。

askopenfilename

这是函数filebutton = tk.Button(text = "Choose file ...", command = lambda: openfile())

openfile

一切正常,文件名被打印到控制台。但是我真正想要的是将文件名存储到一个变量中,这样我就可以像这样用熊猫从excel表格中读取数据:

def openfile():
    filename = tkinter.filedialog.askopenfilename(filetypes = (("Excel", "*.xlsx"), 
                                                               ("All files", "*.*")))
    print(filename)

我该如何实现?还是其他功能还有更好的方法?

1 个答案:

答案 0 :(得分:1)

您无法在按钮函数调用中检索数据。如果您有全局变量,

def openfile():
    global filename = tkinter.filedialog.askopenfilename(filetypes = (("Excel", "*.xlsx"), 
                                                               ("All files", "*.*")))
    print(filename)

,您可以从按钮调用的函数外部访问它。

过去我是通过使用类来解决这个问题的。此tutorial在线将tkinter程序分为几类。如果遵循此结构,则可以创建类变量filename。这样,您就可以在该类的实例中操作文件。