如何在一行中打印所有组合框?Tkinter [英] How to print all combobox in one line ? tkinter

查看:58
本文介绍了如何在一行中打印所有组合框?Tkinter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了几个 tkinter.combobox 对象,并且需要将它们全部打印在一行中.在 Code 1 中显示了我的努力.

I create a few tkinter.combobox objects and need to print them all in one line. In Code 1 shown my effort.

代码1

    import tkinter as tk
    from tkinter import ttk
    
    
    app = tk.Tk()
    app.geometry("800x300")
    
    greeting = tk.Label(text="Kodyfikator")
    greeting.pack()
    
    
    
    list_one = ttk.Combobox(app , values = [1 ,2 ,3]).place(x=10 , y = 50)
    list_two = ttk.Combobox(app , values = [1 ,2 ,3]).place(x=10 , y = 80)
    list_three = ttk.Combobox(app , values = [1 ,2 ,3]).place(x=10 , y = 110)
    list_four = ttk.Combobox(app , values = [1 ,2 ,3]).place(x=10 , y = 140)
    list_five = ttk.Combobox(app , values = [1 ,2 ,3]).place(x=10 , y = 170)
    list_six = ttk.Combobox(app , values = [1 ,2 ,3]).place(x=10 , y = 200)
    list_seven = ttk.Combobox(app , values = [1 ,2 ,3]).place(x=10 , y = 230)
    list_eight = ttk.Combobox(app , values = [1 ,2 ,3]).place(x=10 , y = 260)
    chosenlist = tk.StringVar(list_one)
    
    results = tk.Label(app , text = chosenlist).place(x=200 , y=10) # TODO
    
    app.mainloop()

我将非常感谢您提供任何帮助以取得理想的结果.预先感谢.

I'd appreciate any help to get the desired result. Thanks in advance.

推荐答案

首先,您需要

First you need to seperate your layout manager from your initialize method. then you need a function and a command to invoke it when you like.

那么该功能如何工作?它有一个临时列表,用于存储组合框中的所有值.我已经存储了所有组合框,然后才能遍历此列表.

So how does the function work? It has a temporary list to store all the values in your comboboxes. I had stored all of your comboboxes before to iterate over this list.

->这就是为什么需要分离的原因,否则将是 None

-> That's why its needed to seperat, otherwise it would be None

迭代之后,我们将textvariable设置为存储的内容,并清除列表.注意:我在您的 Label 中添加了可选关键字 textvariable ,以便使用 StringVar()

After iterating we set our textvariable to what we stored and clear the list. Note: I added the optional keyword textvariable to your Label to update it with the StringVar()

import tkinter as tk
from tkinter import ttk


app = tk.Tk()
app.geometry("800x300")

greeting = tk.Label(text="Kodyfikator")
greeting.pack()



list_one = ttk.Combobox(app , values = [1 ,2 ,3])
list_one.place(x=10 , y = 50)
list_two = ttk.Combobox(app , values = [1 ,2 ,3])
list_two.place(x=10 , y = 80)
list_three = ttk.Combobox(app , values = [1 ,2 ,3])
list_three.place(x=10 , y = 110)
list_four = ttk.Combobox(app , values = [1 ,2 ,3])
list_four.place(x=10 , y = 140)
list_five = ttk.Combobox(app , values = [1 ,2 ,3])
list_five.place(x=10 , y = 170)
list_six = ttk.Combobox(app , values = [1 ,2 ,3])
list_six.place(x=10 , y = 200)
list_seven = ttk.Combobox(app , values = [1 ,2 ,3])
list_seven.place(x=10 , y = 230)
list_eight = ttk.Combobox(app , values = [1 ,2 ,3])
list_eight.place(x=10 , y = 260)
chosenlist = tk.StringVar(list_one)

combolst = [list_one,list_two,list_three,list_four,
            list_five,list_six,list_seven,list_eight]

def output():
    stored_output=[]
    for i in combolst:
        stored_output.append(i.get())
    chosenlist.set(f'{stored_output[0]} ,{stored_output[1]} ,'
                   f'{stored_output[2]} ,{stored_output[3]} ,'
                   f'{stored_output[4]} ,{stored_output[5]} ,'
                   f'{stored_output[6]} ,{stored_output[7]}')
    stored_output.clear()

results = tk.Label(app , textvariable = chosenlist).place(x=200 , y=10) #do textvariable
b = tk.Button(app, text='print', command=output)
b.pack()


app.mainloop()

这篇关于如何在一行中打印所有组合框?Tkinter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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