#declare gui object and classes
app = Tk() #creates instance of Tk()
app.title('Check sort DCA') # sets title of gui
#---------------------------------------
def keepSuggested(): #button press actions
es.JournalOut('test2')
def UseNew():
es.JournalOut('test1')
#------------------------------
frame=Frame(app,width=500,height=500)
frame.grid(row=0,column=0)
canvas=Canvas(frame,bg='#FFFFFF',width=500,height=500,scrollregion=(0,0,500,500))
hbar=Scrollbar(frame,orient=HORIZONTAL)
hbar.pack(side=BOTTOM,fill=X)
hbar.config(command=canvas.xview)
vbar=Scrollbar(frame,orient=VERTICAL)
vbar.pack(side=RIGHT,fill=Y)
vbar.config(command=canvas.yview)
canvas.config(width=500,height=500)
canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
canvas.pack(expand=True,fill=BOTH)
spacer1 = Label(canvas, text='|')
spacer2 = Label(canvas, text='|')
spacer3 = Label(canvas, text='|')
spacer4 = Label(canvas, text='|')
spacer5 = Label(canvas, text='|')
Chan_Num = Label(canvas,text='Channel Number')
Chan_Name = Label(canvas, text='Channel Name')
NewChan_Num = Label(canvas, text='New Channel Number')
Set_Title …Run Code Online (Sandbox Code Playgroud) 这是一个create_text:
self.__canvas.create_text(350, lineVotes, text=str(likesPrinted),
font=("calibri", 30), fill="#66FF99", anchor=E)
Run Code Online (Sandbox Code Playgroud)
我怎么能用按钮删除它?
我正在编写一个使用 Tkinter 的 python 应用程序,它的画布用于从调色板绘制图表。图表可能会变得非常大,我目前正在使用鼠标按钮 (ButtonPress-1) 按住并拖动整个画布。
我很难理解如何仅使用箭头键(键盘向上、向下、向左和向右)来实现整个画布的滚动。
帮助!
from tkinter import *
root = Tk()
canvas = Canvas(root, width=400, height= 400, bg="white")
canvas.pack()
rect = canvas.create_rectangle(100, 100, 110, 110, fill='blue')
def move_down(event):
canvas.move(rect, 0, 10)
root.after(1,move_down(event))
root.bind('', move_down)
root.mainloop()
我似乎无法弄清楚如何root.after()的工作原理.我如何解决这个问题,以便矩形继续向下移动?
所以我从我的主管那里得到了一个代码,我在理解方面遇到了问题。我希望使用create_rectangle我提供参数/坐标的方法在光标所在的位置绘制一个矩形:
rect = create_rectangle(x, y, x + 10, y + 10, fill = 'blue', width = 0)
我想x和y这里是我的光标的当前坐标相对于我的根窗口。
在将它们传递给这个函数之前,在我的代码中计算x和y计算的方式是:
x = root.winfo_pointerx() - root.winfo_rootx()
y = root.winfo_pointery() - root.winfo_rooty()
Run Code Online (Sandbox Code Playgroud)
我一生都无法理解为什么要这样做。我试着做
x = root.winfo_pointerx()
y = root.winfo_pointery()
Run Code Online (Sandbox Code Playgroud)
而且也只是
x = root.winfo_rootx()
y = root.winfo_rooty()
Run Code Online (Sandbox Code Playgroud)
但这些都没有绘制光标所在的矩形。我也尝试查看文档,但无法真正理解发生了什么。
那么,为什么x = root.winfo_pointerx() - root.winfo_rootx()和y = root.winfo_pointery() - root.winfo_rooty()正在这里做什么?
我正在开发一个结合了 Tkinter 和 Turtle 模块的简单绘图程序。
我想添加一个选项,用户只需使用类似于 Paint 上的笔小部件的鼠标即可绘制任何内容。我尝试了很多东西,我不知道怎么做。我怎么能让海龟用鼠标在画布上画任何东西(比如 Paint 上的笔小部件)
from tkinter import *
import turtle
sc=Tk()
sc.geometry("1000x1000+100+100")
fr4=Frame(sc,height=500,width=600,bd=4,bg="light green",takefocus="",relief=SUNKEN)
fr4.grid(row=2,column=2,sticky=(N,E,W,S))
#Canvas
canvas = Canvas(fr4,width=750, height=750)
canvas.pack()
#Turtle
turtle1=turtle.RawTurtle(canvas)
turtle1.color("blue")
turtle1.shape("turtle")
points=[]
spline=0
tag1="theline"
def point(event):
canvas.create_oval(event.x, event.y, event.x+1, event.y+1, fill="red")
points.append(event.x)
points.append(event.y)
return points
def canxy(event):
print (event.x, event.y)
def graph(event):
global theline
canvas.create_line(points, tags="theline")
def toggle(event):
global spline
if spline == 0:
canvas.itemconfigure(tag1, smooth=1)
spline = 1
elif spline == 1:
canvas.itemconfigure(tag1, smooth=0)
spline = 0
return spline …Run Code Online (Sandbox Code Playgroud) 我意识到这是一个微不足道的问题,但我仍然感到困惑.
我有一个带有视网膜显示屏的13英寸MacBook Pro.我的系统声称系统报告中的分辨率为2560×1600.我假设tkinter中的Canvas命令测量高度和宽度(以像素为单位),而tk文档似乎也表明了这一点.然而下面的代码,我使用1200作为宽度,700作为tkinter画布的高度,产生一个主要填充我的屏幕的画布.
画布的高度和宽度是多少单位?
from tkinter import *
master = Tk()
x = 1200
y = 700
w = Canvas(master, width=x, height=y)
w.pack()
mainloop()
Run Code Online (Sandbox Code Playgroud) 当使用全屏画布时,如以下代码:
from tkinter import *
root = Tk()
root.attributes("-fullscreen", True)
canvas = Canvas(root, background = "red") # I use a red background for visibility.
canvas.pack(fill = BOTH, expand = True)
Run Code Online (Sandbox Code Playgroud)
我的整个屏幕周围有一个小(2 像素)边框。这种情况发生在我测试代码的任何计算机上。
有没有办法可以删除这个边框,就像让画布填充整个屏幕直到最边缘一样?
谢谢
我制作了一个绘画程序,但我无法顺利绘制并且每次都用不同的名称保存图像。请帮忙!
from tkinter import *
# by Canvas I can't save image, so i use PIL
import PIL
from PIL import Image, ImageDraw
def save():
filename = 'image.png'
image1.save(filename)
def paint(event):
x1, y1 = (event.x), (event.y)
x2, y2 = (event.x + 1), (event.y + 1)
cv.create_oval((x1, y1, x2, y2), fill='black', width=10)
# --- PIL
draw.line((x1, y1, x2, y2), fill='black', width=10)
root = Tk()
cv = Canvas(root, width=640, height=480, bg='white')
# --- PIL
image1 = PIL.Image.new('RGB', (640, 480), 'white')
draw = …Run Code Online (Sandbox Code Playgroud) tkinter paint python-imaging-library python-3.x tkinter-canvas
我想问一下如何检测Python中的标签tkinter.Canvas()。我已经设置了一个示例程序,其中包含一些基本的画布对象,并且所有这些对象都已分配给某个标签。我想要的是,如果我单击该对象但不知道如何执行此操作,则获取该标记值。
例如当我有这个代码时:
import tkinter
canvas = tkinter.Canvas(width=800, height=400)
canvas.pack()
canvas.create_line(20, 100, 150, 350, tags="lines")
canvas.create_line(50, 20, 300, 20, tags="lines")
canvas.create_oval(200, 250, 300, 350, fill="yellow", tags="ovals")
canvas.create_oval(400, 250, 500, 350, fill="blue", tags="ovals")
for j in range(4):
for i in range(10):
canvas.create_rectangle(i * 70 + 10, j * 60 + 10, i * 70 + 60, j * 60 + 50, fill="lightblue", tags=f"square_{j}_{i}")
def click(coords):
print(coords.x, coords.y)
canvas.bind("<Button-1>", click)
Run Code Online (Sandbox Code Playgroud)
我想获得这些值,例如、 、lines等ovals。请问有人知道如何做吗?哦:)square_5_4square_7_1