我无法在变量的组合框中记录选定的值 [英] I am unable to record selected value in combobox in a variable

查看:34
本文介绍了我无法在变量的组合框中记录选定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 tkinter 页面以从 ms-sql 数据库列表中进行选择,并将所选值保存在变量中以供进一步查询.代码如下:

I am trying to create a tkinter page to select from a list of ms-sql databases and save the selected value in a variable for further queries. the code is as below:

import pymssql
from tkinter import StringVar,Tk,OptionMenu,ttk, Button, Label
from sqlalchemy import create_engine,inspect

def get_dbname(event):
    global dbname
    dbname = db_select.get()
    label_dbname["text"] = db_select.get()
   
dbname = ""

master = Tk()

engine = create_engine('mssql+pymssql://(local)')
result = engine.execute("select name FROM sys.databases;")
rows = result.fetchall()
final_result = [list(i) for i in rows]

db_select = ttk.Combobox(master, state = 'readonly')
db_select['values'] = (final_result)
db_select.current(0)
db_select.grid(row = 0, column = 0)

label_dbtitle = Label(master, text = "chosen db = ")
label_dbtitle.grid(row = 1, column = 0)

label_dbname = Label(master, text = "?")
label_dbname.grid(row = 1, column = 1)

buttn1 = Button(master, text = 'Button1')
buttn1.bind("<Button-1>",get_dbname)
buttn1.grid(row = 2, column = 0)

print(dbname)
master.mainloop()

如何将 dbname 保存在变量中?

How can I save the dbname in a variable?

推荐答案

tkinter 这样的 GUI 框架通常是事件驱动"的.这是一种处理范式,其中程序内的逻辑流由事件驱动,例如用户操作、来自其他程序的消息以及来自键盘和鼠标等设备的硬件输入 — 参见 事件驱动编程.这意味着您可能熟悉的程序式或命令式编程方式不能用过.

GUI frameworks like tkinter are usually "event-driven" which is a processing paradigm where the logic flow within the program is driven by events such as user actions, messages from other programs, and hardware inputs from device such as keyboards and mice — see event-driven programming. This means that the procedural or imperative way of programming that you are probably familiar with can't be used.

也就是说,tkinter 也有一个特殊的类型特定对象,它的作用类似于常规 Python 变量,因为它是一个值的容器,例如数字或字符串,它们可以是与许多小部件一起使用,而不是常量文字值.他们会记住所有使用过的小部件,当它们的值发生变化时,所有与它们关联的小部件都会自动更新.

That said, tkinter also has a special type-specific object that acts like a regular Python variable in that it is a container for a value, such as a number or string, and they can be used with many widgets in place of constant literal values. They remember all the widget they were used with and when their value changes, all the widget they are linked to are automatically updated.

在这种情况下,这意味着您可以制作一个 Label,其文本为 StringVar 并且当它的值在您的 get_dbname() 中更改时处理程序,标签显示的内容将自动更新.请注意,在下面的代码中,get_dbname() 不再是绑定到 Button 小部件的 事件处理程序.现在它是一个简单的 command= 回调函数,它是完成同样事情的更推荐和更简单的方法.

In this case it means you can make a Label whose text is a StringVar and when its value is changed in your get_dbname() handler, what's displayed by the label will be updated automatically. Note that in the code below get_dbname() is no longer a <Button-1> event-handler bound to the Button widget. It is now a simple command= callback function for it which is the more commend and simpler way to do accomplish the same thing.

因为我没有你的数据库,所以我在你的问题中评论了处理它的所有代码行,以便我可以运行一些东西.我还稍微更改了小部件的布局,使内容更具可读性 - 但我认为这会让您对我试图解释的概念有一个很好的了解.

Since I don't have your database, I commented all the lines of code in your question dealing with it in order to have something I could run. I also changed the layout of the widgets a little to make things more readable — but I think it will give you a good idea of concepts I've tried to explain.

#import pymssql
from tkinter import Button, Label, OptionMenu, StringVar, Tk, ttk
from tkinter.constants import *
#from sqlalchemy import create_engine,inspect

master = Tk()

def get_dbname():
    dbname.set(db_select.get())  # Will automatically update label_dbname.

dbname = StringVar(value='?')

#engine = create_engine('mssql+pymssql://(local)')
#result = engine.execute("select name FROM sys.databases;")
#rows = result.fetchall()
#final_result = [list(i) for i in rows]
final_result = [f'database #{i}' for i in range(3)]  # Create dummy db names.

db_select = ttk.Combobox(master, state='readonly', values=final_result)
db_select.current(0)
db_select.grid(row=0, column=0, columnspan=2)

buttn1 = Button(master, text='Select', command=get_dbname)
buttn1.grid(row=1, column=0, columnspan=2)

label_dbtitle = Label(master, text="chosen db = ")
label_dbtitle.grid(row=2, column=0, sticky=NE)

label_dbname = Label(master, textvariable=dbname)
label_dbname.grid(row=2, column=1, sticky=NW)

master.mainloop()

这篇关于我无法在变量的组合框中记录选定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆