如何在Python中将两个不同的文本小部件导入一个框架?

时间:2016-02-17 18:22:52

标签: python tkinter tkinter-canvas

抱歉,我是Python的新手,我对此非常困惑。我正在编写一个显示下拉菜单的程序,我希望根据您从下拉菜单中选择的选项显示打印输入。

import random
import tkinter as tk

def roll(n):
    return random.randint(1,n)

def calc():
    thing = var.get()
if (thing == 'Barbarian'):
    print ()

root = tk.Tk()
root.geometry("%dx%d+%d+%d" % (330, 80, 200, 150))
root.title("Gold Calculator")

var = tk.StringVar(root)
var.set('-Select a class-') 

choices = ['Barbarian', 'Bard', 'Cleric', 'Druid','Fighter', 'Monk',     'Paladin', 'Ranger', 'Rougue', 'Sorcerer', 'Warlock', 'Wizard']

option = tk.OptionMenu(root, var, *choices)
option.pack(side='left', padx=10, pady=10)
button = tk.Button(root, text="Calculate Gold", command=calc)
button.pack(side='left', padx=20, pady=10)

root.mainloop()

我想知道如何在下拉菜单右侧创建一个文本框,根据您选择的类打印一些文本。我该怎么做?谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

只需使用tk.Label()小部件及其textvariable参数即可将StringVar绑定到该小部件。每当附加的StringVar更新时,标签将相应地更改其内容:

import random
import tkinter as tk

def roll(n):
    return random.randint(1, n)

def calc():
    thing = var.get()
    if (thing == 'Barbarian'):
        print ()

root = tk.Tk()
root.geometry("%dx%d+%d+%d" % (500, 80, 200, 150))
root.title("Gold Calculator")

selection = tk.StringVar(root)
selection.set('-Select a class-') 


choices = ['Barbarian', 'Bard', 'Cleric', 'Druid', 'Fighter', 'Monk', 'Paladin', 'Ranger', 'Rougue', 'Sorcerer', 'Warlock', 'Wizard']


option = tk.OptionMenu(root, selection, *choices)
option.pack(side='left', padx=10, pady=10)

button = tk.Button(root, text="Calculate Gold", command=calc)
button.pack(side='left', padx=20, pady=10)

label = tk.Label(root, textvariable=selection)
label.pack(side='left', padx=20, pady=10)


root.mainloop()

由于我们开始工作,我们可以看一下如何在标签小部件中获取依赖于选择的文本。为此,我们可以连接一个回调函数,该函数观察与OptionMenu小部件连接的StringVar。在该回调中,我们可以将当前内容作为键读取,并将标签小部件的所需内容作为dict中的值。

请参阅代码中的注释以获取更多详细信息:

import random
import tkinter as tk

def roll(n):
    return random.randint(1, n)

def calc():
    thing = var.get()
    if (thing == 'Barbarian'):
        print ()

# define a callback function in order to set the content of the label widget dependent on the selection
# reads the current content of StringVar `selection`, gets the desired value from the dict `d` and
# writes it to the StringVar `view_text`
def observe_option_menu(*args):
    view_text.set(d.get(selection.get()))

root = tk.Tk()
root.geometry("%dx%d+%d+%d" % (550, 80, 200, 150))
root.title("Gold Calculator")

# create another StringVar and connect a callback function to it.
# hence trance as the 'w' flag, this callback will be fired whenever someone writes to the variable
selection = tk.StringVar(root)
selection.set('-Select a class-') 
selection.trace('w', observe_option_menu)

choices = ['Barbarian', 'Bard', 'Cleric', 'Druid', 'Fighter', 'Monk', 'Paladin', 'Ranger', 'Rougue', 'Sorcerer', 'Warlock', 'Wizard']

# lists `texts` contains sample data in order to get key-value-pairs to combine selection with text of label
texts =  ['VniC', '3DhO', 'CWm0', '8Cf9', 'avNN', 'SUnD', 'lp3R', 'Gtgk', 'FwvV', 'XzH1', 'CyGO', 'UASr']

# create a dict `d` out from the lists given above, you could use a dict directly instead of two seperate lists
d = dict(zip(choices, texts))

view_text = tk.StringVar(root)
view_text.set('Content depends on selection') 

option = tk.OptionMenu(root, selection, *d.keys())
option.pack(side='left', padx=10, pady=10)

button = tk.Button(root, text="Calculate Gold", command=calc)
button.pack(side='left', padx=20, pady=10)

# create a Label widget `label` and place it using pack geometry manager
label = tk.Label(root, textvariable=view_text)
label.pack(side='left', padx=20, pady=10)

root.mainloop()