如何在Tkinter中使变量文本加粗?

时间:2019-04-29 06:33:42

标签: python python-3.x tkinter tkinter-canvas

我有一种情况,我想将Tkinter变量中存在的文本加粗。

代码如下:

best_batsmen = dataset.loc[dataset.loc[dataset['Innings']>=15,'Average'].idxmax(),'Names']
message = ("The best Batsman of the Tournament could possibly be: " + best_batsmen)
canvas_width = 500
canvas_height = 500
root = Toplevel()
root.geometry("700x600")
root.title("Best Batsman")
canvas = Canvas(root, width=canvas_width, height=canvas_height)
canvas.create_text(1, 10, anchor=W, text=message)
img = ImageTk.PhotoImage(Image.open("virat.jpeg"))
canvas.create_image(0, 20, anchor=NW, image=img)
canvas.image = img
canvas.pack()
root.mainloop()

在上面的代码中,我想使用Tkinter仅将best_batsmen变量中存在的文本加粗。谁能帮我解决这个问题。

3 个答案:

答案 0 :(得分:3)

您必须使用第二个var express = require('express'); var tediousExpress = require('express4-tedious'); var app = express(); var Connection = require('tedious').Connection; var Request = require('tedious').Request; app.set('view engine', 'ejs') app.set('views', './views') var config = { 'contains my credentials' } var connection = new Connection(config); app.use(function (req, res, next) { req.sql = tediousExpress(connection); next(); }); app.get('/products', function (req, res ) { /* I want to get column.value object to be rendered to my frontend I already used res.json() here but it returns nothing*/ function executeStatement() { request = new Request("select count * from table where ArticleId= 24588 for json path", function(err, data) { if (err) { console.log(err); } else { console.log('total rows fetched ') ; } connection.close(); }); request.on('row', function(columns) { columns.forEach(function(column) { if (column.value === null) { console.log('NULL'); } else { column.value ; } }); }); connection.execSql(request) ; } }); 来仅将文本create_textbest_batsman放在

font='bold'

但是您必须手动计算canvas.create_text(1, 10, anchor='w', text=message) canvas.create_text(380, 10, anchor='w', text=best_batsmen, font='bold') (即x)的best_batsmen位置

380

最终,您可以尝试使用import tkinter as tk from PIL import Image, ImageTk best_batsmen = "James Bond" message = "The best Batsman of the Tournament could possibly be: " canvas_width = 500 canvas_height = 500 root = tk.Tk() canvas = tk.Canvas(root, width=canvas_width, height=canvas_height) canvas.pack() canvas.create_text(1, 10, anchor='w', text=message) canvas.create_text(380, 10, anchor='w', text=best_batsmen, font='bold') img = ImageTk.PhotoImage(Image.open("virat.jpeg")) canvas.create_image(0, 20, anchor='nw', image=img) #canvas.image = img root.mainloop()

计算位置
tkinter.font.Font().measure()

如果您将文字放在图片上方,因此不需要透明背景的文字,则可以使用import tkinter.font as tkfont #width = tkfont.Font(family='arial', size=20, weight='normal').measure(message) width = tkfont.Font().measure(message) canvas.create_text(width, 10, anchor='w', text=best_batsmen, font='bold') 放置Frame,并使用{将两个pack()放在Labels内{1}}

Frame

通过这种方式,您不必计算pack(side='left')的{​​{1}}位置

您可以将图像放在frame = tk.Frame(root) frame.pack() tk.Label(frame, text=message).pack(side='left') tk.Label(frame, text=best_batsmen, font='bold').pack(side='left') 位置

x

或者您可以使用best_batsmen将其放在画布上。创建(0, 0)后,您必须这样做。

import tkinter as tk
from PIL import Image, ImageTk

best_batsmen = "James Bond"
message = "The best Batsman of the Tournament could possibly be: "

canvas_width = 500
canvas_height = 500

root = tk.Tk()

frame = tk.Frame(root)

tk.Label(frame, text=message).pack(side='left')
tk.Label(frame, text=best_batsmen, font='bold').pack(side='left')
frame.pack()

canvas = tk.Canvas(root, width=canvas_width, height=canvas_height)
canvas.pack()

img = ImageTk.PhotoImage(Image.open("virat.jpeg"))
canvas.create_image(0, 0, anchor='nw', image=img)
#canvas.image = img

root.mainloop()

如果要在上方而不是上方放置图片,则必须在create_window(..., window=frame)之后使用Canvas。但是小部件(frame = tk.Frame(root) canvas.create_window(0, 0, window=frame, anchor='nw') tk.Label(frame, text=message).pack(side='left') tk.Label(frame, text=best_batsmen, font='bold').pack(side='left') ,“标签”等)不能具有透明背景。

creat_window

您可以将create_image与带有已分配颜色和权重的标签一起使用,而不是Frame

答案 1 :(得分:1)

您可以在画布上创建文本并编辑其字体样式。

canvas.create_text(1, 10, font=('arial', 20, BOLD), text='your text here')

为此,您必须从tkinter字体导入样式。

from tkinter.font import BOLD

答案 2 :(得分:0)

您可以将邮件分为两部分:

best_batsmen = 'Virat Kohli'
message = 'The best Batsman of the Tournament could possibly be: '

然后照常绘制第一部分:

msg = canvas.create_text(1, 10, text=message, anchor=W)

使用返回的项目ID msg获取message的边界框和字体:

bbox = canvas.bbox(msg) # get the message bounding box
# import tkinter.font as tkfont
font = tkfont.Font(font=canvas.itemcget(msg, 'font')) # get the message font

然后将字体的粗细设置为'bold',并使用粗体样式在第一部分的末尾绘制第二部分:

font['weight'] = 'bold' # change font weight to bold
# show best_batsmen at the end of the message with bold style
canvas.create_text(bbox[2], bbox[1], text=best_batsmen, font=font, anchor=NW)