简体   繁体   English

如何将tkinter条目用作函数的参数

[英]How do I use a tkinter entry as a parameter for a function

I would like a text box to ask for input in a tkinter window, then use that input as a parameter to call a function that draws a Sierpinski triangle. 我想要一个文本框在tkinter窗口中要求输入,然后使用该输入作为参数来调用绘制Sierpinski三角形的函数。 My buttons work but my input box does not. 我的按钮有效,但我的输入框无效。 I keep trying to fix my code but it is not working, any help would be appreciated. 我一直在尝试修复我的代码,但是它不起作用,任何帮助将不胜感激。

import tkinter as tk
from tkinter import *

root = tk.Tk()
frame = tk.Frame(root)
frame.pack()
root.title('Fractals') #titles the button box

top_frame = tk.Frame()
mid_frame = tk.Frame()

prompt_label = tk.Label(top_frame, \
       text='Enter a number of iterations (more is better):')
iterations = tk.Entry(root,bd=1)

itr=iterations.get()
itr=int(itr)

button = tk.Button(frame, 
                   text="QUIT", 
                   fg="red",
                   command=quit)
button.pack(side=tk.LEFT)

sTriangle = tk.Button(frame,
                   text="Triangle",
                   command=lambda: sierpinski(fred, (-500,-500), (500,-500), 
(0,500),itr))
sTriangle.pack(side=tk.LEFT)

fsquare = tk.Button(frame,
                    text="Square",
                    command=fractalsquare(fred,(-500,-500),(500,-500), 
(500,500),(-500,500),itr))
fsquare.pack(side=tk.LEFT)

root.mainloop()

There are several issues: 有几个问题:

1) Choose one way to import tkinter or confusion will result 1)选择一种导入tkinter的方式,否则会导致混乱

2) You should provide a master for your Frames and then pack them. 2)您应该为相框提供一个母版,然后包装它们。 Pay attention on where the frames appear and what they contain. 请注意框架的显示位置及其包含的内容。

3) It's usual to assign a textvariable to the Entry which will contain what you enter into it. 3)通常将一个textvariable分配给Entry,其中将包含您输入的内容。 The textvariable should be a tk.StringVar. textvariable应该是tk.StringVar。

4) If a Button has a callback function, it must be defined before you create the button. 4)如果Button具有回调函数,则必须在创建按钮之前定义它。

5) The variable fred is not defined. 5)没有定义变量fred。

Example of how you can write it: 如何编写示例:

import tkinter as tk

root = tk.Tk()
root.title('Fractals') #titles the button box

# Create the Label at the top
top_frame = tk.Frame(root)  # Top Frame for 
top_frame.pack()
prompt_label = tk.Label(top_frame,
                        text='Enter a number of iterations (more is better):')
prompt_label.pack()

# Create the Entry in the middle
mid_frame = tk.Frame(root)
mid_frame.pack()
itr_string = tk.StringVar()
iterations = tk.Entry(mid_frame,textvariable=itr_string)
iterations.pack()

fred=None   # Was not defined...

# Create Buttons at the bottom
bot_frame = tk.Frame(root)
bot_frame.pack()
button = tk.Button(bot_frame, text="QUIT", fg="red", command=quit)
button.pack(side=tk.LEFT)

def sierpinski(*args):  # sTriangle button callback function
    itr = int(itr_string.get())   # How to get text from Entry
    # if Entry does not contain an integer this will throw an exception

sTriangle = tk.Button(bot_frame, text="Triangle",
    command=lambda: sierpinski(fred, (-500,-500), (500,-500),(0,500),itr_string))
sTriangle.pack(side=tk.LEFT)

def fractalsquare(*args): pass     # fsquare button callback function

fsquare = tk.Button(bot_frame, text="Square", command=fractalsquare(fred,
    (-500,-500),(500,-500),(500,500),(-500,500),itr_string))
fsquare.pack(side=tk.LEFT)

root.mainloop()

You should seriously study a basic tkinter tutorial. 您应该认真学习基本的tkinter教程。 Try this one: An Introduction To Tkinter 试试这个: Tkinter简介

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM