是否有create_text()支持自动换行的模式或技术?我一直在使用create_text()vs. aLabel或Text小部件,因为我将文本放在画布上的图像顶部。
此外,是否有一个 Tkinter API 可以截断不适合特定宽度的文本,例如后缀之类的省略号,例如。Where very, very, very long text被转换为类似Where very, very, ....
我正在使用UI类中的一个函数,它是一个配置窗口,它显示程序的徽标,并在底部显示一个更新文本,告诉您它正在加载什么,等等。这是我到目前为止的内容:
self.window = "config"
self.windowWidth = 340
self.windowHeight = 270
infoText = "Configuring Kh..."
self.root = tk.Tk()
self.root.geometry("%dx%d+400+400" % (self.windowWidth, self.windowHeight))
self.root.title("Kh Control v1.1 starting...")
logo = tk.PhotoImage(file="KhLogo.gif")
mainPanel = tk.Canvas(self.root, width=self.windowWidth, height=self.windowHeight)
mainPanel.image = logo
mainPanel.pack()
mainPanel.create_image(0, 0, image=logo, anchor="nw")
mainPanel.create_text(0,200, text=infoText, anchor="nw", fill="yellow")
return
Run Code Online (Sandbox Code Playgroud)
我希望infoText中的文本水平居中并垂直向下偏移200px。垂直偏移效果很好,但我不知道如何水平放置文本居中。
我从尝试使用年龄较大的((width / 2)-(str length / 2))开始,但后来意识到每个字母都不是1px。而anchor =“ center”似乎仅将文本的一半放在屏幕的左侧。
我是Python的新手(仅几天),因此,如果我缺少明显的内容,这就是原因。
编辑:并且如果它不是很明显,则此文本将更改,因此我不能仅对偏移量做出绝对决定,它必须随文本更改
我已经接近了,但是有两个问题。一个是旋转正方形的最终位置(不再居中)。第二个是我的原始顶点列表发生了变化,尽管我使用list(..)进行了复制。
像往常一样感谢任何帮助。
from Tkinter import *
import math
WIDTH = 400
HEIGHT = 400
CANVAS_MID_X = WIDTH/2
CANVAS_MID_Y = HEIGHT/2
SIDE = WIDTH/4
root = Tk()
canvas = Canvas(root, bg="black", height=HEIGHT, width=WIDTH)
canvas.pack()
vertices = [
[CANVAS_MID_X - SIDE/2, CANVAS_MID_Y - SIDE/2],
[CANVAS_MID_X + SIDE/2, CANVAS_MID_Y - SIDE/2],
[CANVAS_MID_X + SIDE/2, CANVAS_MID_Y + SIDE/2],
[CANVAS_MID_X - SIDE/2, CANVAS_MID_Y + SIDE/2]]
def rotate(points, angle):
new_points = list(points)
rad = angle * (math.pi/180)
cos_val = math.cos(rad)
sin_val = math.sin(rad)
for coords in …Run Code Online (Sandbox Code Playgroud) 我定义了GCanvas,它是Canvas的扩展.我的目的是在类级别绑定到GCanvas.它不起作用.
我也尝试绑定到tk.Canvas,它也不起作用.绑定到rootGCanvas实例或与GCanvas实例绑定工作正常.(这些替代方案对我来说都没有用,但我只是试着看看发生了什么.)运行OS X,El Capitan.
import Tkinter as tk
class GCanvas(tk.Canvas, object):
def __init__(self, master, **kwargs):
tk.Canvas.__init__(self, master, kwargs)
@staticmethod
def enter(e):
print "enter", e.widget, e.x, e.y
@staticmethod
def leave(e):
print "leave", e.widget
@staticmethod
def motion(e):
print "motion", e.widget, e.x, e.y
approach = "bindinstance"
root = tk.Tk()
gc = GCanvas(root, width=400, height=300)
print "root is", root, "gc is", gc
gc.pack()
if approach == "bindGCanvas":
print "binding to GCanvas"
root.bind_class(GCanvas, '<Enter>', GCanvas.enter)
root.bind_class(GCanvas, '<Leave>', GCanvas.leave)
#root.bind_class(GCanvas, '<Motion>', GCanvas.motion)
elif approach …Run Code Online (Sandbox Code Playgroud) 到目前为止我有这个代码。起初我让它画圆圈,效果很好。我以为画矩形会很容易,但我只能画正方形。我希望有不同宽度和长度的形状。我知道我需要更改 (x1, y1, x2, y2) 的框,但我该怎么做呢?
def down(event): # A mouse event will be passed in with x and y attributes
global startx, starty # Use global variables for assignment
startx = event.x # Store the mouse down coordinates in the global variables
starty = event.y
def up(event):
tk_color_string = color(red_intvar, green_intvar, blue_intvar)
r = (startx-event.x)**2 + (starty-event.y)**2 # Pythagorean theorem
r = int(r**.5) # square root to get distance
new_shape = canvas.create_rectangle(startx-r, starty-r, startx+r, starty+r,
fill=tk_color_string, outline='#000000')
shapes.append(new_shape) # …Run Code Online (Sandbox Code Playgroud) 我想在 tkinter 的画布上使用图像,但我无法用 Pillow 打开图像。事实上,我的所有图像都与我的代码位于同一个文件夹中,但是当我放置“icon.png”时它不起作用。然后,当我将完整路径放入图像 (C:/Users/myName/Desktop/PythonWork/game/src/icon.png) 时,它就可以工作了。
File "F:\Python\lib\site-packages\PIL\Image.py", line 2312, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'icon.png'
因此我的问题是:如何使相对路径起作用?
在此先感谢您的帮助 :)
我有Canvas很多按钮,它们的 ID 存储在字典中。画布很长,有垂直滚动条。有没有办法自动将视图定位在给定的按钮上?
当使用 时Text,txt.see(position)通常可以工作,但我看到Canvas没有see。
唯一可能的选择似乎是.focus(),但cv.focus(ID)似乎没有做我想做的
我正在尝试使用 tkinter 中的 Canvas 在我的 python 应用程序中插入图像。相同的代码是:
class Welcomepage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
canvas = tk.Canvas(self, width = 1000, height = 1000, bg = 'blue')
canvas.pack(expand = tk.YES, fill = tk.BOTH)
image = tk.PhotoImage(file="ice_mix.gif")
canvas.create_image(480, 258, image = image, anchor = tk.NW)
Run Code Online (Sandbox Code Playgroud)
图像正在从源读取,但仍未显示在框架中。我是 GUI 编程的新手,请帮助我。
题
如何创建圆角文本小部件?我想我有一个创建圆形画布的想法,并用没有边框的文本框填充整个画布.
问题
当我尝试扩展画布的高度和宽度时,创建圆形边框画布的代码不起作用.线断裂并变得不连续.
创建圆角画布的代码取自如何制作带圆角的tkinter画布矩形?
我的代码具有自定义宽度和高度
def rounded_rect(canvas, x, y, w, h, c):
canvas.create_arc(x, y, x+2*c, y+2*c, start= 90, extent=90, style="arc")
canvas.create_arc(x+w-2*c, y+h-2*c, x+w, y+h, start=270, extent=90, style="arc")
canvas.create_arc(x+w-2*c, y, x+w, y+2*c, start= 0, extent=90, style="arc")
canvas.create_arc(x, y+h-2*c, x+2*c, y+h, start=180, extent=90, style="arc")
canvas.create_line(x+c, y, x+w-c, y )
canvas.create_line(x+c, y+h, x+w-c, y+h )
canvas.create_line(x, y+c, x, y+h-c)
canvas.create_line(x+w, y+c, x+w, y+h-c)
import tkinter
root = tkinter.Tk()
canvas = tkinter.Canvas(root)
canvas.grid(row=0,column=0)
# The width and height has been changed from …Run Code Online (Sandbox Code Playgroud) 我需要在 tkinter 中以画布矩形为中心放置一个文本/标签。
首先,我有一个覆盖整个屏幕的画布(800、600)。然后我有几个矩形,我使用以下方法制作:
create_rectangle(...)
Run Code Online (Sandbox Code Playgroud)
第一个矩形的第一个 X 是 275,第二个 X 是 525。
第一个矩形的第一个 Y 是 265,第二个 Y 是 315。
menuBtn1 = canvas.create_rectangle(275, 165, 525, 215, fill="#C2B6BF")
Run Code Online (Sandbox Code Playgroud)
现在我如何在这个矩形的中心放置一个文本/标签?