【问题标题】:How to print results iteratively on a canvas or text widget within a GUI made in Python?如何在 Python 制作的 GUI 内的画布或文本小部件上迭代打印结果?
【发布时间】:2021-10-31 00:17:45
【问题描述】:

我制作了一个简单的 GUI,它有两个按钮,分别是方形功能和退出按钮。我想将结果打印在画布上,如预期结果所示。

这是我的代码

from tkinter import *
from PIL import Image,ImageTk
from tkinter import filedialog
from PIL import Image, ImageTk
import os
import cv2
import numpy as np
import time


    class application(Tk):
        def __init__(self,parent):
            Tk.__init__(self,parent)
            self.parent = parent
            self.minsize(width=300,height=500)
            self.initialize()
            
        def initialize(self):
            self.grid_columnconfigure(0,weight=1)
            self.grid_columnconfigure(1,weight=1)
            self.grid_columnconfigure(2,weight=1)
            
            #Button widgets
            ###########################################################################################
            self.button1 = Button(self,text='Square',bg= 'blue',width=15,height=2, command =Square)
            self.button1.grid(column=0,row=1,sticky='W',pady=5)
            
            self.button3 = Button(self,text='Exit',bg= 'blue',width=10,height=2,command=self.destroy)
            self.button3.grid(column=1,row=5,sticky='W',pady=5)
            
            #Text widget for inserting the result
        ############################################################################################
        
        self.canvas = Canvas(self,width=230,height=180,state=NORMAL)
        self.canvas.grid(column=0,row=4,sticky='W',padx=100,pady=10)
        #self.canvas.configure(bg='green')
        
        #Label widget
        ############################################################################################

        
        self.label3 = Label(self,text="Square",bg = 'red',width=10,height=2,anchor="center")
        self.label3.grid(column=0,row=3,sticky='W',padx=120)
def Square(self):
    for i in range(1,10):
        y = i**2
        print(i, y)
        
if __name__ == "__main__":
    app = application(None)
    #font.nametofont('TkDefaultFont').configure(size=10)
    app['bg']='red'
    app.title("square")
    app.mainloop()

我的预期结果如下图所示

【问题讨论】:

    标签: python user-interface tkinter canvas text


    【解决方案1】:

    不完全确定您在问什么。但是,如果您只想在画布上创建文本,请使用 canvas.create_text(x, y, text)

    from tkinter import *
    from PIL import Image,ImageTk
    
    
    class application(Tk):
        def __init__(self,parent):
            Tk.__init__(self,parent)
            self.parent = parent
            self.minsize(width=300,height=500)
            self.initialize()
            
        def initialize(self):
            self.grid_columnconfigure(0,weight=1)
            self.grid_columnconfigure(1,weight=1)
            self.grid_columnconfigure(2,weight=1)
            
            #Button widgets
            ###########################################################################################
            self.button1 = Button(self,text='Square',bg= 'blue',width=15,height=2, command=self.Square)
            self.button1.grid(column=0,row=1,sticky='W',pady=5)
            
            self.button3 = Button(self,text='Exit',bg= 'blue',width=10,height=2,command=self.destroy)
            self.button3.grid(column=1,row=5,sticky='W',pady=5)
            
            #Text widget for inserting the result
        ############################################################################################
            
            self.canvas = Canvas(self,width=230,height=180,state=NORMAL)
            self.canvas.grid(column=0,row=4,sticky='W',padx=100,pady=10)
            #self.canvas.configure(bg='green')
            
            self.label3 = Label(self,text="Square",bg = 'red',width=10,height=2,anchor="center")
            self.label3.grid(column=0,row=3,sticky='W',padx=120)
            
        def Square(self):
            for i in range(1,10):
                y = i**2
                print(i, y)
                c_id = self.canvas.create_text(0, 20, text=f"{i}\t{y}", fill="blue", font="Times 14")
                bbox = self.canvas.bbox(c_id)
                self.canvas.coords(c_id, 100, bbox[3]*i)
    
    if __name__ == "__main__":
        app = application(None)
        #font.nametofont('TkDefaultFont').configure(size=10)
        app['bg']='red'
        app.title("square")
        app.mainloop()
    

    【讨论】:

    • 我在我的 GUI 上创建了这样一个画布,我想要的是函数的答案应该迭代地打印在该画布上。问题是,当我单击按钮时,它会打印最后一个答案 9、81,而不是 1、1 中的所有答案。你现在明白了吗?
    • @KimwagaMakono 但是我给你的代码是从 1、1 打印出来的。你在你的代码中尝试过上面的 sn-p 吗?
    • 它给出了 9、81。我把我的代码和你的代码结合起来了,它也不打印任何东西。
    • @KimwagaMakono 你点击方形按钮了吗?你是合并还是替换它?
    • 我删除了我的功能并把你的
    猜你喜欢
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多