簡體   English   中英

Python,tkinter:如何在要求用戶輸入之前顯示圖像?

[英]Python, tkinter: How do I display an image before I ask 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()

不要使用input因為它在等待input會阻塞線程(GUI 使用的主線程)。 相反,使用Entry作為input ,使用Label作為print因為無論如何這是一個GUI

# 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()

你的縮進被搞砸了。 我不知道這是問題所在,還是在您發布代碼時發生的,但這對我有用

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_image()函數后,通過執行以下操作刷新Tk()

display_image()
root.update()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM