【问题标题】:Using functions within a GUI - how to pass and return variables to widgets在 GUI 中使用函数 - 如何将变量传递和返回到小部件
【发布时间】:2016-05-17 01:46:39
【问题描述】:

已解决 - 找到答案 @How to take input from Tkinter

ONE GOTCHA - 我尝试在已解决的链接中复制示例的样式,尽管我保留了一些自己的格式。我发现如果获取返回值的对象是在创建时添加了任何其他点,例如放置 .pack() 或在我的情况下是 .grid() 在创建对象的同一行,当您尝试将变量传回时,它会抛出一个错误。所以,继续使用第二行到 .pack().grid()

原帖

我有一个在命令行上运行良好的程序,我正在尝试给它一个 GUI。不幸的是,我仍然对如何在界面中使用函数/方法/命令感到困惑。

我的示例是在获得用户输入后使用按钮让所有内容滚动。在命令行程序中,它是作为变量传递给一个函数并返回三个其他变量的用户输入。这三个变量被传递给另一个函数以提供结果,然后将结果格式化以显示。

好吧,我正在尝试使用 tkinter。我有一个输入字段,它将用户输入分配给一个变量。我有一个链接到一个函数的按钮,该函数用于启动球滚动......我不知道如何将该变量发送到所需的函数,或者如何获取返回的变量并应用它。在我的具体示例中,我想将变量“notation”发送到函数“parse()”,“parse()”的输出将发送到“roll()”,“roll()”的输出发送到变量“输出”,然后显示。所有这一切都将使用带有“command=calculate”的按钮开始,“calculate()”是一个让整个球滚动的函数。

非常抱歉...我完全是自学成才的,而且我确信我什至没有使用正确的术语来解决很多问题。我也明白这个例子不是很pythonic - 我最终会将所有这些放入类中并将函数更改为方法,但现在我只想看到它工作。

这是目前为止的代码。公平的警告,这不是我遇到的唯一问题......我只想坚持一个问题,直到我能解决它。

#!/usr/bin/env python

from tkinter import *
from tkinter import ttk
import re
import random

# Parsing function from the original command line tool
# Turns dice notation format into useful variables

def parse(d):
    dice, dtype_mod = d.split('d')

    dnum = 1
    dtype = 6
    mod = 0

    if dtype_mod:
        if '-' in dtype_mod:
            dtype, mod = dtype_mod.split('-')
            mod = -1 * int(mod)
        elif '+' in dtype_mod:
            dtype, mod = dtype_mod.split('+')
            mod = int(mod)
        else:
            dtype = dtype_mod
    if not dtype: dtype = 6
    if not mod: mod = 0

    return (int(dice), int(dtype), int(mod))

# Rolling function from the original command line tool
# 'print()' will be changed into a variable, with the output 
# appended in the working version.

def roll(a, b):
    rolls = []
    t = 0

    for i in range(a):
        rolls.append(random.randint(1, b))
        t += int(rolls[i])
        print(('Roll number %d is %s, totaling %d') % (i + 1, rolls[i], t))
    return (int(t))

# Placeholder - the rest of the command line code will be used here later
# This code will be what starts everything rolling.
# For debugging, attempting to pass a variable, doing it wrong.

def calculate():
    output = "this is something different"
    return output

# Initialize

dice = Tk()
dice.title('Roll the Dice')
dice.geometry("800x600+20+20")

# Drawing the main frame

mainframe = ttk.Frame(dice, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)

# Variables needed for program and widgets

random.seed()
notation = ""
output = """This is
an example of a lot of crap
that will be displayed here
if I ever get
this to work
and this is a really long line -Super Cali Fragil Istic Expi Ali Docious
"""

# Dice notation entry field, and it's label

notation_entry = ttk.Entry(mainframe, width=10, textvariable=notation)
notation_entry.grid(column=2, row=1)
ttk.Label(mainframe, text="Dice").grid(column=1, row=1)

# Section used for output
"""Huge laundry list of problems here:

1.  textvariable is not displaying anything here.  If I change it to text
    it seems to work, but from what I can tell, that will not update.
2.  I would love for it to have a static height, but height is not allowed here.
    Need to figure out a workaround.
3.  Also, have not figured out how to get the value returned by calculate()
    to show up in here when the button is pressed..."""

output_message = Message(mainframe, textvariable=output, width= 600)
output_message.grid(column=1, row=2, rowspan=3, columnspan=3)

# The 'make it go' button.
"""Can I pass the function a variable?"""

ttk.Button(mainframe, text="Roll!", command=calculate).grid(column=3, row=5)


# This is a bunch of stuff from the command line version of this program.
# Only here for reference

"""while True:
    notation = raw_input('Please input dice notation or q to quit: ')

    if notation == "q":
        raise SystemExit
    else:
         print(notation)

        numbers = parse(notation)
        (dice, dtype, mod) = numbers

        total = roll(dice, dtype)
        total += mod

        print('Your total is %d' % total)"""


dice.mainloop()

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    当您在创建对象时使用 textvariable 时,需要使用字符串变量而不是普通字符串变量,我将您的变量表示法更改为字符串变量

    notation = StringVar()
    notation.set("")
    

    然后在我使用的计算子中获取符号的值

    print(notation.get())
    

    我编辑的你的代码被完整粘贴了

    #!/usr/bin/env python
    
    from tkinter import *
    from tkinter import ttk
    import re
    import random
    
    # Parsing function from the original command line tool
    # Turns dice notation format into useful variables
    
    def parse(d):
        dice, dtype_mod = d.split('d')
    
        dnum = 1
        dtype = 6
        mod = 0
    
        if dtype_mod:
            if '-' in dtype_mod:
                dtype, mod = dtype_mod.split('-')
                mod = -1 * int(mod)
            elif '+' in dtype_mod:
                dtype, mod = dtype_mod.split('+')
                mod = int(mod)
            else:
                dtype = dtype_mod
        if not dtype: dtype = 6
        if not mod: mod = 0
    
        return (int(dice), int(dtype), int(mod))
    
    # Rolling function from the original command line tool
    # 'print()' will be changed into a variable, with the output 
    # appended in the working version.
    
    def roll(a, b):
        rolls = []
        t = 0
    
        for i in range(a):
            rolls.append(random.randint(1, b))
            t += int(rolls[i])
            print(('Roll number %d is %s, totaling %d') % (i + 1, rolls[i], t))
        return (int(t))
    
    # Placeholder - the rest of the command line code will be used here later
    # This code will be what starts everything rolling.
    # For debugging, attempting to pass a variable, doing it wrong.
    
    def calculate():
        print(notation.get())
        output = "this is something different"
        return output
    
    # Initialize
    
    dice = Tk()
    dice.title('Roll the Dice')
    dice.geometry("800x600+20+20")
    
    # Drawing the main frame
    
    mainframe = ttk.Frame(dice, padding="3 3 12 12")
    mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
    mainframe.columnconfigure(0, weight=1)
    mainframe.rowconfigure(0, weight=1)
    
    # Variables needed for program and widgets
    
    random.seed()
    notation = StringVar()
    notation.set("")
    output = """This is
    an example of a lot of crap
    that will be displayed here
    if I ever get
    this to work
    and this is a really long line -Super Cali Fragil Istic Expi Ali Docious
    """
    
    # Dice notation entry field, and it's label
    
    notation_entry = ttk.Entry(mainframe, width=10, textvariable=notation)
    notation_entry.grid(column=2, row=1)
    ttk.Label(mainframe, text="Dice").grid(column=1, row=1)
    
    # Section used for output
    """Huge laundry list of problems here:
    
    1.  textvariable is not displaying anything here.  If I change it to text
        it seems to work, but from what I can tell, that will not update.
    2.  I would love for it to have a static height, but height is not allowed here.
        Need to figure out a workaround.
    3.  Also, have not figured out how to get the value returned by calculate()
        to show up in here when the button is pressed..."""
    
    output_message = Message(mainframe, textvariable=output, width= 600)
    output_message.grid(column=1, row=2, rowspan=3, columnspan=3)
    
    # The 'make it go' button.
    """Can I pass the function a variable?"""
    
    ttk.Button(mainframe, text="Roll!", command=calculate).grid(column=3, row=5)
    
    
    # This is a bunch of stuff from the command line version of this program.
    # Only here for reference
    
    """while True:
        notation = raw_input('Please input dice notation or q to quit: ')
    
        if notation == "q":
            raise SystemExit
        else:
             print(notation)
    
            numbers = parse(notation)
            (dice, dtype, mod) = numbers
    
            total = roll(dice, dtype)
            total += mod
    
            print('Your total is %d' % total)"""
    
    
    dice.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2013-10-08
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 2012-09-30
      • 2020-06-21
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多