无法获取组合框值来启用/禁用按钮 Python Tkinter

问题描述 投票:0回答:1

我有 2 个组合框,国家和年份。他们可以分别更改 2 个不同的标签。 findbutton 按钮(靠近代码底部)使他们能够使用 def searchget() 来执行此操作。这是因为标签使用 StringVars,该 StringVars 根据组合框中输入的内容而变化,def 通过上述按钮更新它们。所以,这部分工作正常。但我仍然困惑的是,如果为每个或两个选择的组合框值为 0(国家 = --COUNTRY-- 和/或年份 = --YEAR-),如何阻止用户按下查找按钮(禁用它) -)。 .current(0) 是否使它不起作用?我仍然希望默认值为 0,但如果用户想要搜索某些内容,则年份和国家/地区都必须是其他内容。

这是我的全部代码,以防组合框、标签、def 或按钮以外的其他东西影响它:

#!/bin/python3

#7f7f7f, #679969, #c3c3c3

#Imports
import tkinter as tk
from tkinter import ttk
from tkinter import *

#Windows
root = Tk()
root.title("GovGrants")

#Window Interface
root.geometry("400x500")
root['bg'] = "#7f7f7f"

#defs
def searchget():
    countryTitle.set(country.get())
    yearTitle.set(year.get())
    root.update()

def options():
    OPTIONS = Toplevel(root)
    OPTIONS.geometry("400x500")
    OPTIONS['bg'] = "#7f7f7f"
    optionsTitle = Label(OPTIONS, text = "Does nothing so far.\nSorry.", bg = "#c3c3c3").place(x=10, y=10)
    

#Icons and text
#root.wm_attributes('-transparentcolor', '#679969')
MainTitle = Label(root,
                  text = "Enterprise and Small Business Grants and\nSupport from the Governments (By Country)", font=("Arial Bold", 10),
                  width = 44, height = 3, borderwidth = 3, relief = "solid",
                  bg = "#c3c3c3").place(x=20, y=10)



#Buttons, dropdowns etc
    
#Combobox country
a = tk.StringVar() 
country = ttk.Combobox(root, width = 20, textvariable = a)
#w15
   
country['values'] = (' --COUNTRY--',  
                     ' Australia',  
                     ' Austria', 
                     ' Belgium', 
                     ' Canada', 
                     ' Czechia', 
                     ' Denmark', 
                     ' Estonia', 
                     ' Finland', 
                     ' France', 
                     ' Germany', 
                     ' Greece', 
                     ' Hungary',
                     ' Ireland',
                     ' Israel',
                     ' Italy',
                     ' Japan',
                     ' Korea',
                     ' Latvia',
                     ' Lithuania',
                     ' Luxembourg',
                     ' Netherlands',
                     ' New Zealand',
                     ' Norway',
                     ' Poland',
                     ' Portugal',
                     ' Slovak Republic',
                     ' Slovenia',
                     ' Spain',
                     ' Sweden',
                     ' Switzerland',
                     ' United Kingdom',
                     ' United States',
                     ' Referance Area: Non-OECD economies > Bulgaria',
                     ' Croatia',
                     ' Romania',
                     ' Russia',
                     ' South Africa') 
  
country['state'] = 'readonly'
country.place(x=80, y=70)
country.current(0)

#Combobox year
b = tk.StringVar() 
year = ttk.Combobox(root, width = 10, textvariable = b)
#w5
   
year['values'] = (' --YEAR--',    
                  ' 2002',
                  ' 2003', 
                  ' 2004', 
                  ' 2005', 
                  ' 2006', 
                  ' 2007', 
                  ' 2008', 
                  ' 2009', 
                  ' 2011', 
                  ' 2012', 
                  ' 2013', 
                  ' 2014', 
                  ' 2015', 
                  ' 2016', 
                  ' 2017', 
                  ' 2018', 
                  ' 2019', 
                  ' 2020', 
                  ' 2021', 
                  ' 2022', 
                  ' 2023') 
                           
  
year['state'] = 'readonly'
year.place(x=237, y=70) 
year.current(0)

#.get()
countryTitle = StringVar()
countryTitle.set(country.get())
yearTitle = StringVar()
yearTitle.set(year.get())

#grant actually showing
dataTitle = Label(root, text = "Data:", bg = "#7f7f7f", fg = "#FFFFFF").place(x=20, y=95)
businessSTitle = Label(root, text = "Business Stage", bg = "#7f7f7f", fg = "#FFFFFF").place(x=20, y=120)

countryTitlea = Label(root, textvariable = countryTitle, bg = "#7f7f7f", fg = "#FFFFFF").place(x=120, y=135)
yearTitlea = Label(root, textvariable = yearTitle, bg = "#7f7f7f", fg = "#FFFFFF").place(x=200, y=135)

seedTitle = Label(root, text = "Seed", font=("TkDefaultFont", 9), width = 9, height = 1, borderwidth = 2, relief = "solid", bg = "#c3c3c3").place(x=99, y=160)
startupTitle = Label(root, text = "Start-up", font=("TkDefaultFont", 9), width = 9, height = 1, borderwidth = 2, relief = "solid", bg = "#c3c3c3").place(x=165, y=160)
latestageTitle = Label(root, text = "Late-stage", font=("TkDefaultFont", 9), width = 9, height = 1, borderwidth = 2, relief = "solid", bg = "#c3c3c3").place(x=232, y=160)
totalTitle = Label(root, text = "Total", font=("TkDefaultFont", 9), width = 9, height = 1, borderwidth = 2, relief = "solid", bg = "#c3c3c3").place(x=299, y=160)
GATitle = Label(root, text = "Grant Average\n($US Million)", borderwidth = 1, relief = "solid", bg = "#c3c3c3").place(x=20, y=180)
adTitle = Label(root, text = "$", width = 9, height = 2, borderwidth = 1, relief = "solid", bg = "#c3c3c3", anchor="w").place(x=99, y=180)
bdTitle = Label(root, text = "$", width = 9, height = 2, borderwidth = 1, relief = "solid", bg = "#c3c3c3", anchor="w").place(x=166, y=180)
cdTitle = Label(root, text = "$", width = 9, height = 2, borderwidth = 1, relief = "solid", bg = "#c3c3c3", anchor="w").place(x=233, y=180)
ddTitle = Label(root, text = "$", width = 9, height = 2, borderwidth = 1, relief = "solid", bg = "#c3c3c3", anchor="w").place(x=300, y=180)

#Extra-type
businessTTitle = Label(root, text = "Business Type", bg = "#7f7f7f", fg = "#FFFFFF").place(x=20, y=230)

countryTitleb = Label(root, textvariable = countryTitle, bg = "#7f7f7f", fg = "#FFFFFF").place(x=120, y=250)
yearTitleb = Label(root, textvariable = yearTitle, bg = "#7f7f7f", fg = "#FFFFFF").place(x=200, y=250)

assistanceTitle = Label(root, text = "Assistance", font=("TkDefaultFont", 9), width = 9, height = 1, borderwidth = 2, relief = "solid", bg = "#c3c3c3").place(x=165, y=270)
fundingTitle = Label(root, text = "Funding", font=("TkDefaultFont", 9), width = 9, height = 1, borderwidth = 2, relief = "solid", bg = "#c3c3c3").place(x=232, y=270)
itemTitle = Label(root, text = "Business", font=("TkDefaultFont", 9), width = 9, height = 1, borderwidth = 2, relief = "solid", bg = "#c3c3c3").place(x=98, y=270)
at = Label(root, text = " \n \n \n \n \n \n \n \n", width = 9, borderwidth = 1, relief = "solid", bg = "#c3c3c3", anchor="w").place(x=166, y=290)
ft = Label(root, text = " \n \n \n \n \n \n \n \n", width = 9, borderwidth = 1, relief = "solid", bg = "#c3c3c3", anchor="w").place(x=233, y=290)
it = Label(root, text = " \n \n \n \n \n \n \n \n", width = 9, borderwidth = 1, relief = "solid", bg = "#c3c3c3", anchor="w").place(x=99, y=290)

#Buttons
optionsButton = Button(root, text = "Settings", width = 6, command = options).place(x=20, y=68)
findButton = Button(root, text = "Search", width = 6, command = searchget).place(x=329, y=68)

if country.get() == '--COUNTRY--':
    findbutton['state'] = 'disabled';

elif year.get() == '--YEAR--':
    findbutton['state'] = 'disabled';

#41 to 20 (21)
#boxes #c3c3c3

root.mainloop()

#######################################################

我搜索了所有代码问题网站、w3schools 和其他“如何做”网站,但似乎没有任何效果。我怀疑这与获取组合框信息的方式有关(我尝试过country.get()= 0,country =“--COUNTRY--”,country.get()=“--COUNTRY--” ,(还有更多,但实际上只是引号和常见答案的不同组合)几乎所有我可能可以的组合。)或按钮如何更改状态(再次尝试 findbutton = "DISABLED", findbutton['state' ] = 'disabled'(只是引用和所有常见答案的不同组合)。当我单击按钮时,不会出现错误,但就像 if 语句不存在一样。

顺便说一句:不涉及 if 语句的答案也很有帮助,我真的只是陷入困境,无法在任何地方找到解决方案。一切都是“禁用组合框”或“禁用另一个按钮的按钮”,无论我交换和更改哪些部分,这两者都不起作用。

python tkinter button combobox disable
1个回答
0
投票

您可以将虚拟事件

<<ComboboxSelected>>
绑定到两个组合框,并根据事件回调中这些组合框的选定值更新
findButton
的状态。

还要避免类似以下声明:

findButton = Button(...).place(...)

因为

findButton
将会是
None
,因为它是
.place(...)
的结果。

您的案例所需的更改:

...

findButton = Button(root, text="Search", width=6, command=searchget)
findButton.place(x=329, y=68)

def check_combos(event=None):
    state = 'disabled' if (a.get() == country['values'][0] or b.get() == year['values'][0]) else 'normal'
    findButton.config(state=state)

country.bind('<<ComboboxSelected>>', check_combos)
year.bind('<<ComboboxSelected>>', check_combos)

check_combos() # set button state initially
root.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.