📜  循环浏览 Tkinter 中的按钮

📅  最后修改于: 2022-05-13 01:54:37.560000             🧑  作者: Mango

循环浏览 Tkinter 中的按钮

在本文中,让我们看看如何在 Tkinter 中遍历按钮。

分步实施:

步骤 1:导入 Tkinter 包及其所有模块并创建一个根窗口(root = Tk())。

Python3
# Import package and it's modules
from tkinter import *
  
# create root window
root = Tk()
  
# root window title and dimension
root.title("GeekForGeeks")
  
# Set geometry (widthxheight)
root.geometry('400x400')
  
# Execute Tkinter
root.mainloop()


Python3
# Import package and it's modules
from tkinter import *
  
# create root window
root = Tk()
  
# root window title and dimension
root.title("GeekForGeeks")
  
# Set geometry (widthxheight)
root.geometry('400x400')
  
# Entry Box
text = Entry(root, width = 30, bg = 'White')
text.pack(pady = 10)
  
# Execute Tkinter
root.mainloop()


Python3
# Import package and it's modules
from tkinter import *
  
  
# text_update function
def text_updation(language):
    text.delete(0, END)
    text.insert(0, language)
  
# create root window
root = Tk()
  
# root window title and dimension
root.title("GeekForGeeks")
  
# Set geometry (widthxheight)
root.geometry('400x400')
  
# Entry Box
text = Entry(root, width=30, bg='White')
text.pack(pady=10)
  
# create buttons
button_dict = {}
words = ["Python", "Java", "R", "JavaScript"]
for lang in words:
    
    # pass each button's text to a function
    def action(x = lang): 
        return text_updation(x)
        
    # create the buttons 
    button_dict[lang] = Button(root, text = lang,
                               command = action)
    button_dict[lang].pack(pady=10)
  
# Execute Tkinter
root.mainloop()


输出



第 2 步:现在让我们添加一个 Entry() 类,一旦单击其中一个按钮,就会显示按钮名称。

蟒蛇3

# Import package and it's modules
from tkinter import *
  
# create root window
root = Tk()
  
# root window title and dimension
root.title("GeekForGeeks")
  
# Set geometry (widthxheight)
root.geometry('400x400')
  
# Entry Box
text = Entry(root, width = 30, bg = 'White')
text.pack(pady = 10)
  
# Execute Tkinter
root.mainloop()

输出

第 3 步:现在让我们创建一个空字典 (button_dict) 来保存所有按钮对象和一个由所有按钮名称组成的列表。现在遍历列表中的每个项目以创建它的按钮对象并将其存储在字典中。对于按钮命令,创建一个名为“action”的函数,并为每个按钮调用text_update()函数来更新之前创建的 Entry() 对象中条目的更改。

蟒蛇3

# Import package and it's modules
from tkinter import *
  
  
# text_update function
def text_updation(language):
    text.delete(0, END)
    text.insert(0, language)
  
# create root window
root = Tk()
  
# root window title and dimension
root.title("GeekForGeeks")
  
# Set geometry (widthxheight)
root.geometry('400x400')
  
# Entry Box
text = Entry(root, width=30, bg='White')
text.pack(pady=10)
  
# create buttons
button_dict = {}
words = ["Python", "Java", "R", "JavaScript"]
for lang in words:
    
    # pass each button's text to a function
    def action(x = lang): 
        return text_updation(x)
        
    # create the buttons 
    button_dict[lang] = Button(root, text = lang,
                               command = action)
    button_dict[lang].pack(pady=10)
  
# Execute Tkinter
root.mainloop()

输出

循环通过按钮 tkinter