python中tkinter中value_python – 从Tkinter中的combobox中获取所选值

我使用Tkinter在

python中创建了一个简单的组合框,我想检索用户选择的值.搜索之后,我想我可以通过绑定选择事件并调用一个将使用类似box.get()的函数来完成此操作,但这不起作用.程序启动时,会自动调用该方法,并且不会打印当前选择.当我从组合框中选择任何项目时,不会调用任何方法.这是我的代码片段:

self.box_value = StringVar()

self.locationBox = Combobox(self.master, textvariable=self.box_value)

self.locationBox.bind("<>", self.justamethod())

self.locationBox['values'] = ('one', 'two', 'three')

self.locationBox.current(0)

这是我从框中选择项目时应该调用的方法:

def justamethod (self):

print("method is called")

print (self.locationBox.get())

谁能告诉我如何获得所选值?

编辑:我已经纠正了对justamethod的调用,通过在将盒子绑定到James Kent建议的函数时删除括号.但现在我收到了这个错误:

TypeError:justamethod()只取1个参数(给定2个)

编辑2:我已经发布了这个问题的解决方案.

谢谢.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用法举例:从数据表name字段写入VALComboBox的items,id字段写入VALComboBoxvalues,当从VALComboBox选择一个选项后,就可以从value属性获得相应的ID,或者写value为某ID,VALComboBox将定位在相应的选项上;unit VALComboBox;interfaceuses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms, Menus, Dialogs, StdCtrls;type TValComboBox = class(TComboBox) private FValue: PString; FValues: TStrings; FOnChange: TNotifyEvent; function GetValue: string; function GetButtonValue(Index: Integer): string; procedure SetValue(const Value: string); procedure SetValues(Value: TStrings); protected procedure Change; dynamic; procedure Notification(AComponent: TComponent; Operation: TOperation); override; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; property Value: string read GetValue write SetValue; property ItemIndex; published property Values: TStrings read FValues write SetValues; property OnChange: TNotifyEvent read FOnChange write FOnChange; end;procedure Register;implementationconstructor TValComboBox.Create(AOwner: TComponent);begin inherited Create(AOwner); FValue := NullStr; FValues := TStringList.Create; style := csDropDownList;end;destructor TValComboBox.Destroy;begin DisposeStr (FValue); FValues.Free; inherited Destroy;end;procedure TValComboBox.Notification(AComponent: TComponent; Operation: TOperation);begin inherited Notification(AComponent, Operation);end;function TValComboBox.GetValue : string;begin result:=values[itemindex];end;function TValComboBox.GetButtonValue(Index: Integer): string;begin if (Index < FValues.Count) and (FValues[Index] ‘‘) then Result := FValues[Index] else if (Index < Items.Count) then Result := Items[Index] else Result := ‘‘;end;procedure TValComboBox.SetValue (const Value: string);var I : Integer;begin AssignStr(FValue, Value); if (ItemIndex < 0) or (GetButtonValue(ItemIndex) Value) then begin if (ItemIndex >= 0) then ItemIndex := -1; for I := 0 to Items.Count - 1 do begin if GetButtonValue(I) = Value then begin ItemIndex := I; break; end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值