你的代码中有一个问题,就是在打开Excel文件时,你使用了字符串 `'choose_file()'` 作为文件名,而不是调用 `choose_file()` 函数返回的文件路径。要解决这个问题,可以将 `choose_file()` 函数的返回值传递给 `open()` 方法。
另外,你需要将 Excel 文件的读取操作放在 `choose_file()` 函数内部,因为只有在用户选择了文件之后才能进行读取操作。以下是修改后的代码:
```python
from tkinter import *
from tkinter import filedialog
import xlwings as xw
def choose_file():
file_path = filedialog.askopenfilename()
print(file_path)
app = xw.App()
wb = app.books.open(file_path)
names = wb.sheets['Sheet1'].range('d2:d12').value
wb.close()
app.quit()
for n in names:
print(n)
root = Tk()
button = Button(root, text="Choose File", command=choose_file)
button.pack()
root.mainloop()
```
这个程序会创建一个窗口,并在窗口中添加一个“Choose File”按钮。当用户点击按钮时,会弹出一个文件选择对话框,用户可以在其中选择一个 Excel 文件。选择完毕后,程序会打印出该文件中 Sheet1 的 D2:D12 单元格范围内的所有值。