从 Tkinter 中获取变量 [英] Getting variable out of Tkinter

查看:29
本文介绍了从 Tkinter 中获取变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下是否有人知道如何从 Tkinter 中的 Entry 中取出一个变量以用于将来的计算.

I would like to ask if anyone knows how to get out a variable from an Entry in Tkinter to be used in future calculation.

让我们假设我想创建一个提示,用户需要在其中放置两个数字在两个不同的 Entry 小部件中.

Let us assume that I want to create a prompt where the user needs to place two numbers in the two different Entry widgets.

这些数字将在另一个脚本中用于计算.如何从 Tkinter 中创建的提示中检索值?

These numbers are to be used in another script for calculation. How can I retrieve the values from the prompt created in Tkinter?

在我看来,我需要使用下面的代码创建一个函数,并让它从 Tkinter 提示符返回值.但是,我无法返回数字,因为我正在破坏根窗口.我怎样才能通过这个,最好没有全局变量.

In my opinion, I would need to create a function with the code bellow and make it return the value from the Tkinter prompt. However, I cannot return the numbers because I'm destroying the root window. How can I get pass this, preferably without global variables.

最好的问候

from tkinter import *
from tkinter import ttk

#Start of window
root=Tk()
#title of the window
root.title('Title of the window')


def get_values():
    values=[(),(value2.get())]

    return values




# Creates a main frame on the window with the master being the root window
mainframe=ttk.Frame(root, width=500, height=300,borderwidth=5, relief="sunken")
mainframe.grid(sticky=(N, S, E, W))


###############################################################################
#
#
# Label of the first value
label1=ttk.Label(master=mainframe, text='First Value')
label1.grid(column=0,row=0)

# Label of the second value
label2=ttk.Label(master=mainframe, text='Second Value')
label2.grid(column=0,row=1)




###############################################################################
#
#
# Entry of the first value
strvar1 = StringVar()
value1 = ttk.Entry(mainframe, textvariable=strvar1)
value1.grid(column=1,row=0)

# Entry of the second value
strvar2 = StringVar()
value2 = ttk.Entry(mainframe, textvariable=strvar2)
value2.grid(column=1,row=1)


# Creates a simplle button widget on the mainframe
button1 = ttk.Button(mainframe, text='Collect', command=get_values)
button1.grid(column=2,row=1)



# Creates a simplle button widget on the mainframe
button2 = ttk.Button(mainframe, text='Exit', command=root.destroy)
button2.grid(column=2,row=2)




root.mainloop()

推荐答案

您使用类是因为类实例及其变量在 tkinter 退出后仍然存在.https://www.tutorialspoint.com/python/python_classes_objects.htm 你可能想重新检查你的一些文档要求,即当声明是"root.title('Title of the window')",加上解释"#title of the window"简直是浪费你的时间..

You use a class because the class instance and it's variables remain after tkinter exits.https://www.tutorialspoint.com/python/python_classes_objects.htm And you may want to reexamine some of your documentation requirements, i.e. when the statement is "root.title('Title of the window')", adding the explanation "#title of the window" is just a waste of your time..

""" A simplified example
"""

import sys
if 3 == sys.version_info[0]:  ## 3.X is default if dual system
    import tkinter as tk     ## Python 3.x
else:
    import Tkinter as tk     ## Python 2.x

class GetEntry():
    def __init__(self, master):
        self.master=master
        self.entry_contents=None
        self.e = tk.Entry(master)
        self.e.grid(row=0, column=0)
        self.e.focus_set()


        tk.Button(master, text="get", width=10, bg="yellow",
               command=self.callback).grid(row=10, column=0)

    def callback(self):
        """ get the contents of the Entry and exit
        """
        self.entry_contents=self.e.get()
        self.master.quit()

master = tk.Tk()
GE=GetEntry(master)
master.mainloop()

print("\n***** after tkinter exits, entered =", GE.entry_contents)

这篇关于从 Tkinter 中获取变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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