我有一个python代码段,它使用REST API来提取数据。该函数取3个参数,即URL,USERNAME,PASSWORD。现在我想要一个Gui用于相同的场景。我使用tkinter但无法弄清楚按钮如何调用带参数的方法。 这是示例python代码:
def example(url, user, password):
with OciRest(url, user, password) as oci:
storages = oci.get('assets/storages')
for storage in storages:
print('Storage %s : %s' % (storage['family'], storage['name']))
storage_pools = oci.get(storage['self'] + '/storagePools')
for storage_pool in storage_pools:
print(' SP %s: %.3fTB' %(storage_pool['name'],
storage_pool['capacity']['total']['value'] / 1024.0 / 1024.0))
if __name__ == '__main__':
options = parse_command_line()
example(options.url, options.user, options.password)
现在我想要具有url,用户名,密码的GUI,当我点击提交按钮时,代码应该调用。 提前谢谢。
答案 0 :(得分:1)
最好的解决方案是让你的按钮调用一个函数来获取值并调用你的函数。这样做,您不必在按钮命令中传递参数。
def on_button():
url = urlEntry.get()
username = usernameEntry.get()
password = passwordEntry.get()
example(url, username, password)
...
button = tk.Button(..., command=on_button)