将结果打印到画布而不是shell窗口



Attached是我在Tkinter中使用画布构建的GUI的一部分,因此可以在后台插入图像。当我调用函数Relay_1时:结果被发送到Python shell窗口。我想要的是画布中的一个文本框,并在画布上而不是在外壳中显示结果(即打印结果)。任何想法都将不胜感激。

import Tkinter
#Function 
def Relay_1():
arduinoData.write(b'1')
print ("This is a testn")   
class ProgramGUI:
def __init__(self):
# Creates the Main window for the program        
self.main = tkinter.Tk()
# Change Default ICON
self.main.iconbitmap(self,default = 'test.ico')        
# Create title, window size and make it a non re sizable window       
self.main.title('Test')
self.main.resizable(False, False)                 
self.main.geometry('680x300')       
self.canvas = tkinter.Canvas(self.main, width = 680, height = 300)
self.canvas.pack()
self.logo = tkinter.PhotoImage(file = 'test.png')
self.canvas.create_image(0,0,image = self.logo, anchor = 'nw')
# Create 3 Frame to group the widgets        
self.top = tkinter.Frame(self.main)
self.middle = tkinter.Frame(self.main)                
self.bottom = tkinter.Frame(self.main)

等等

tkinter画布小部件有一个非常简单易用的方法来绘制名为create_text()的文本。你可以这样用,

self.canvas.create_text(10, 10, text='This is a testn')

可以通过传递包括fontfilljustify在内的广泛参数来定制文本。请在此处查看可通过参数的完整列表:http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.create_text-方法

要在执行代码时添加文本,可以在ProgramGUI()方法中创建一个类:

def draw_text(self, text):
self.canvas.create_text(10, 10, text=text)

并在创建对象后使用它。

最新更新