Python,tkinter:在请求用户输入之前,如何显示图像?

Python,tkinter:在请求用户输入之前,如何显示图像?,python,tkinter,Python,Tkinter,我正在处理一个需要显示图像的项目,然后继续要求用户输入,以定义图像中标注的特定点的距离。 使用我现在的代码,它只会在用户输入完成后显示图像。在请求用户输入之前,如何使图像显示 图片: 当前代码: # File: farmer_john_field # Author: Elijah Cherry # Course: CS-1010 # Original Problem: Draw specified image and calculate area of darkened region from

我正在处理一个需要显示图像的项目,然后继续要求用户输入,以定义图像中标注的特定点的距离。 使用我现在的代码,它只会在用户输入完成后显示图像。在请求用户输入之前,如何使图像显示

图片:

当前代码:

# File: farmer_john_field
# Author: Elijah Cherry
# Course: CS-1010
# Original Problem: Draw specified image and calculate area of darkened region

from tkinter import *
from tkinter import ttk
import math

root = Tk()
win = Canvas(root, width = 500, height = 500)
win.grid()

def main():
    def display_image():
        # point a = 200,200
        # point b = 300,200
        # point c = 300,300
        # point d = 200,300

        # points move clockwise from top left (north west) quadrant

        # rectangle to fill rear area
        rectangle_back = win.create_rectangle (200,200,  300,300, fill="gray")

        # circles will be placed by top left corner and bottom right corner
        circle_a = win.create_oval (200-50, 200-50,   200+50, 200+50, fill="white")
        #                           a  xtl, a  ytl    a  xbr  a  ybr
        circle_b = win.create_oval (300-50, 200-50,   300+50, 200+50, fill="white")
        #                           b  xtl, b  ytl    b  xbr  b  ybr
        circle_c = win.create_oval (300-50, 300-50,   300+50, 300+50, fill="white")
        #                           c  xtl, c  ytl    c  xbr  c  ybr
        circle_d = win.create_oval (200-50, 300-50,   200+50, 300+50, fill="white")
        #                           d  xtl, d  ytl    d  xbr  d  ybr

        # rectangle outline
        rectangle_outline = win.create_rectangle (200,200,  300,300, outline="gray")

        # texts (labels for points a b c d)
        text_a = win.create_text (200,200, anchor="se", text="A", fill="black")
        text_b = win.create_text (300,200, anchor="sw", text="B", fill="black")
        text_c = win.create_text (300,300, anchor="nw", text="C", fill="black")
        text_d = win.create_text (200,300, anchor="ne", text="D", fill="black")

    display_image()

    def calcn():
        # collect length information
        length = float(input("Enter length of one side of the square ABCD: "))
        radius = (length/2)
        dark_area_result = math.pi * radius**(2)
        print ("Area of shaded region =","{:0.2f}".format(dark_area_result))

    calcn()

main()

你的压痕弄错了。我不知道这是否是问题所在,或者是在你发布代码时发生的,但这对我来说是有效的

from tkinter import *
from tkinter import ttk
import math

root = Tk()
win = Canvas(root, width = 500, height = 500)
win.grid()

def display_image():

        # point a = 200,200
        # point b = 300,200
        # point c = 300,300
        # point d = 200,300

        # points move clockwise from top left (north west) quadrant

        # rectangle to fill rear area
    rectangle_back = win.create_rectangle (200,200,  300,300, fill="gray")

        # circles will be placed by top left corner and bottom right corner
    circle_a = win.create_oval (200-50, 200-50,   200+50, 200+50, fill="white")
        #                           a  xtl, a  ytl    a  xbr  a  ybr
    circle_b = win.create_oval (300-50, 200-50,   300+50, 200+50, fill="white")
        #                           b  xtl, b  ytl    b  xbr  b  ybr
    circle_c = win.create_oval (300-50, 300-50,   300+50, 300+50, fill="white")
        #                           c  xtl, c  ytl    c  xbr  c  ybr
    circle_d = win.create_oval (200-50, 300-50,   200+50, 300+50, fill="white")
        #                           d  xtl, d  ytl    d  xbr  d  ybr

        # rectangle outline
    rectangle_outline = win.create_rectangle (200,200,  300,300, outline="gray")

        # texts (labels for points a b c d)
    text_a = win.create_text (200,200, anchor="se", text="A", fill="black")
    text_b = win.create_text (300,200, anchor="sw", text="B", fill="black")
    text_c = win.create_text (300,300, anchor="nw", text="C", fill="black")
    text_d = win.create_text (200,300, anchor="ne", text="D", fill="black")


def calcn():

        # collect length information
        length = float(input("Enter length of one side of the square ABCD: "))
        radius = (length/2)
        dark_area_result = math.pi * radius**(2)
        print ("Area of shaded region =","{:0.2f}".format(dark_area_result))

display_image()
calcn()
给您的
Tk()
命令以显示某些内容:

运行
display\u image()
函数后,通过执行以下操作刷新
Tk()

display_image()
root.update()

不要使用
input
,因为它会在等待输入时阻塞线程(GUI使用的主线程)。相反,使用
条目
作为
输入
,使用
标签
作为
打印
,因为这是一个GUI:


我想知道这是否与他在函数中定义和调用函数有关。谢谢,缩进是在这里输入函数时发生的。代码工作正常,但在请求用户输入之前,它仍然不会显示图像。发布您现在使用的代码,因为我发布的代码工作正常。测试您的代码后,在您向
calcn()
输入之前,不会显示Tk。哦,我的错误。在底部添加为root.mainloop()。如果仍然不起作用,请使用Tkinter条目获取值,将所有内容都保留在Tkinter中。谢谢!我从程序中删除了main()并将root.update()放在display_image()和calcn()之间。现在它正是我想要的!上面的代码无法生成独立的GUI,因为它缺少
mainloop
# File: farmer_john_field
# Author: Elijah Cherry
# Course: CS-1010
# Original Problem: Draw specified image and calculate area of darkened region

from tkinter import *
from tkinter import ttk
import math

root = Tk()
win = Canvas(root, width = 500, height = 500)
win.grid()

def main():
    def display_image():
        # point a = 200,200
        # point b = 300,200
        # point c = 300,300
        # point d = 200,300

        # points move clockwise from top left (north west) quadrant

        # rectangle to fill rear area
        rectangle_back = win.create_rectangle (200,200,  300,300, fill="gray")

        # circles will be placed by top left corner and bottom right corner
        circle_a = win.create_oval (200-50, 200-50,   200+50, 200+50, fill="white")
        #                           a  xtl, a  ytl    a  xbr  a  ybr
        circle_b = win.create_oval (300-50, 200-50,   300+50, 200+50, fill="white")
        #                           b  xtl, b  ytl    b  xbr  b  ybr
        circle_c = win.create_oval (300-50, 300-50,   300+50, 300+50, fill="white")
        #                           c  xtl, c  ytl    c  xbr  c  ybr
        circle_d = win.create_oval (200-50, 300-50,   200+50, 300+50, fill="white")
        #                           d  xtl, d  ytl    d  xbr  d  ybr

        # rectangle outline
        rectangle_outline = win.create_rectangle (200,200,  300,300, outline="gray")

        # texts (labels for points a b c d)
        text_a = win.create_text (200,200, anchor="se", text="A", fill="black")
        text_b = win.create_text (300,200, anchor="sw", text="B", fill="black")
        text_c = win.create_text (300,300, anchor="nw", text="C", fill="black")
        text_d = win.create_text (200,300, anchor="ne", text="D", fill="black")


    def display_the_query():
        query_label = Label(root,
                        text="Enter length of one side of the square ABCD: ")
        query_entry = Entry(root)
        # to be able to track the entry text
        # . notation to attach it as an attribute
        query_entry.var = StringVar()
        # to attaching the attribute as the displayed text
        query_entry['textvariable'] = query_entry.var
        result_label = Label(root)
        # to actually track the input each time there's a difference
        # which essentially allows dynamically calculating the result
        query_entry.var.trace_add('write',
            lambda *_, var=query_entry.var, lbl=result_label: calcn(var, lbl))
        query_label.grid()
        query_entry.grid()
        result_label.grid()

    def calcn(var, result_label):
        user_input = var.get()
        if user_input:
            length = float(user_input)
            radius = length / 2
            dark_area_result = math.pi * radius**(2)
            result_label['text'] = "Area of shaded region = {:0.2f}".format(
                                                            dark_area_result)


    display_image()
    display_the_query()

main()
mainloop()