我正在做一个项目,需要显示一个图像,然后继续要求用户输入一个定义在图像中的特定点的距离。 使用我现在的代码,它只会在用户输入完成后显示图像。在请求用户输入之前,如何使图像显示?在
图片:image to display before asking for user input
当前代码:
# 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()
给您的
Tk()
命令以显示某些内容:运行
display_image()
函数后,请执行以下操作刷新Tk()
:不要使用}作为{},因为这是一个GUI:
input
,因为它在等待输入时阻塞了线程(GUI使用的主线程)。相反,使用Entry
作为input
,而{你的压痕被弄坏了。我不知道这是问题还是你发布代码时发生的,但这对我有用
相关问题 更多 >
编程相关推荐