我想自动调整文本大小

问题描述 投票:0回答:1
import matplotlib.pyplot as plt
import numpy as np
import tkinter as tk
from tkinter import *
root = tk.Tk()
root.config(bg='white')
root.state('zoomed')
x = np.arange(-10, 11, 1)
root.title('Fonksiyonlar')
c1 = Canvas(height=600, width=1100, bg='gray97')
import time

errortext = tk.Label(text='Girdiğiniz Sayıları Kontrol Ediniz', font='Calibri 25')

def gos():
    errortext.place_forget()

def b1c():
    def b3g():
        for i in e1.get():
            if i != '0' or '1' or '2' or '3' or '4' or '5' or '6' or '7' or '8' or '9' or '0' or '.':
                e1.delete(0, END)

                errortext.place(x=300, y=200)
                root.after(3000, gos)

    bbir.destroy()
    biki.destroy()
    c1.destroy()

    e1 = tk.Entry(highlightcolor='blue', bg='pink', font='Calibri 40')
    e1.place(width=230, height=230, x=80, y=300)
    bşş = tk.Button(text='TAMAM', command=b3g)
    bşş.pack()
    lx2 = tk.Label(text='x² + ', font='Calibri 150', bg='white')
    lx2.place(x=350, y=300)
    e2 = tk.Entry(highlightcolor='blue', bg='pink', font='Calibri 40')
    e2.place(width=230, height=230, x=700, y=300)
    lx3 = tk.Label(text='x +', font='Calibri 150', bg='white')
    lx3.place(x=1000, y=300)
    e3 = tk.Entry(highlightcolor='blue', bg='pink', font='Calibri 40')
    e3.place(width=230, height=230, x=1280, y=300)
    lx4 = tk.Label(text='=', font='Calibri 150', bg='white')
    lx4.place(x=1550, y=300)
    e4 = tk.Entry(highlightcolor='blue', bg='pink', font='Calibri 40')
    e4.place(width=230, height=230, x=1680, y=300)
    b1 = tk.Button(text='1', padx=20, pady=20, font='Calibri 20')
    b1.place(x=850, y=570)
    b2 = tk.Button(text='2', padx=20, pady=20, font='Calibri 20')
    b2.place(x=950, y=570)
    b3 = tk.Button(text='3', padx=20, pady=20, font='Calibri 20')
    b3.place(x=1050, y=570)
    b4 = tk.Button(text='4', padx=20, pady=20, font='Calibri 20')
    b4.place(x=850, y=700)
    b5 = tk.Button(text='5', padx=20, pady=20, font='Calibri 20')
    b5.place(x=950, y=700)
    b6 = tk.Button(text='6', padx=20, pady=20, font='Calibri 20')
    b6.place(x=1050, y=700)
    b7 = tk.Button(text='7', padx=20, pady=20, font='Calibri 20')
    b7.place(x=850, y=820)
    b8 = tk.Button(text='8', padx=20, pady=20, font='Calibri 20')
    b8.place(x=950, y=820)
    b9 = tk.Button(text='9', padx=20, pady=20, font='Calibri 20')
    b9.place(x=1050, y=820)
    b0 = tk.Button(text='0', padx=75, pady=20, font='Calibri 15')
    b0.place(x=850, y=930)
    beş = tk.Button(text='=', padx=22, pady=20, font='Calibri 20')
    beş.place(x=1050, y=930)
    bar = tk.Button(text='+', padx=20, pady=20, font='Calibri 20')
    bar.place(x=1150, y=570)
    bek = tk.Button(text='-', padx=22, pady=20, font='Calibri 20')
    bek.place(x=1150, y=700)
    bb = tk.Button(text='/', padx=22, pady=20, font='Calibri 20')
    bb.place(x=1150, y=930)
    bç = tk.Button(text='*', padx=20, pady=20, font='Calibri 20')
    bç.place(x=1150, y=820)

    print(e1.select_present())

c1.place(x=420, y=170)
bbir = Button(text='1.dereceden denklemler', bg='gray91', padx=300, pady=30, font='Calibri 30', command=b1c)
bbir.place(x=450, y=300)
biki = Button(text='2.dereceden denklemler', bg='gray91', padx=300, pady=30, font='Calibri 30')
biki.place(x=450, y=500)

def hesapla(a, b, c):

    return a * x ** 2 + b * x + c

m = 0.5 * x + x ** 2 + 5

plt.plot(x, hesapla(0, 0, 0))
plt.grid()

plt.show()

我想限制条目中的字符,当用户添加新字符时,它应该更小以获得最佳外观。

python tkinter tkinter-entry
1个回答
0
投票

要在用户添加字符时自动调整 Entry 小部件中文本的大小,您可以使用回调函数中的事件参数,并根据输入文本的长度调整字体大小。此代码将事件绑定到 Entry 小部件的 b3g 函数,因此每次按下按键时,都会调用该函数,并根据输入文本的长度调整字体大小。

这是我做的,如果对你有用,请告诉我,

import matplotlib.pyplot as plt
import numpy as np
import tkinter as tk
from tkinter import *

root = tk.Tk()
root.config(bg='white')
root.state('zoomed')
x = np.arange(-10, 11, 1)
root.title('Fonksiyonlar')
c1 = Canvas(height=600, width=1100, bg='gray97')

errortext = tk.Label(text='Girdiğiniz Sayıları Kontrol Ediniz', font='Calibri 25')

def gos():
    errortext.place_forget()

def b1c():
    def b3g(event=None):
        for i in e1.get():
            if i not in {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.'}:
                e1.delete(0, END)
                errortext.place(x=300, y=200)
                root.after(3000, gos)

        current_text = e1.get()
        font_size = max(40 - len(current_text), 10)
        e1.config(font=f'Calibri {font_size}')

    bbir.destroy()
    biki.destroy()
    c1.destroy()

    e1 = tk.Entry(root, highlightcolor='blue', bg='pink', font='Calibri 40')
    e1.place(width=230, height=230, x=80, y=300)
    bşş = tk.Button(root, text='TAMAM', command=b3g)
    bşş.pack()

    lx2 = tk.Label(root, text='x² + ', font='Calibri 150', bg='white')
    lx2.place(x=350, y=300)
    e2 = tk.Entry(root, highlightcolor='blue', bg='pink', font='Calibri 40')
    e2.place(width=230, height=230, x=700, y=300)
    lx3 = tk.Label(root, text='x +', font='Calibri 150', bg='white')
    lx3.place(x=1000, y=300)
    e3 = tk.Entry(root, highlightcolor='blue', bg='pink', font='Calibri 40')
    e3.place(width=230, height=230, x=1280, y=300)
    lx4 = tk.Label(root, text='=', font='Calibri 150', bg='white')
    lx4.place(x=1550, y=300)
    e4 = tk.Entry(root, highlightcolor='blue', bg='pink', font='Calibri 40')
    e4.place(width=230, height=230, x=1680, y=300)

    b1 = tk.Button(root, text='1', padx=20, pady=20, font='Calibri 20')
    b1.place(x=850, y=570)
    b2 = tk.Button(root, text='2', padx=20, pady=20, font='Calibri 20')
    b2.place(x=950, y=570)
    b3 = tk.Button(root, text='3', padx=20, pady=20, font='Calibri 20')
    b3.place(x=1050, y=570)
    b4 = tk.Button(root, text='4', padx=20, pady=20, font='Calibri 20')
    b4.place(x=850, y=700)
    b5 = tk.Button(root, text='5', padx=20, pady=20, font='Calibri 20')
    b5.place(x=950, y=700)
    b6 = tk.Button(root, text='6', padx=20, pady=20, font='Calibri 20')
    b6.place(x=1050, y=700)
    b7 = tk.Button(root, text='7', padx=20, pady=20, font='Calibri 20')
    b7.place(x=850, y=820)
    b8 = tk.Button(root, text='8', padx=20, pady=20, font='Calibri 20')
    b8.place(x=950, y=820)
    b9 = tk.Button(root, text='9', padx=20, pady=20, font='Calibri 20')
    b9.place(x=1050, y=820)
    b0 = tk.Button(root, text='0', padx=75, pady=20, font='Calibri 15')
    b0.place(x=850, y=930)
    beş = tk.Button(root, text='=', padx=22, pady=20, font='Calibri 20')
    beş.place(x=1050, y=930)
    bar = tk.Button(root, text='+', padx=20, pady=20, font='Calibri 20')
    bar.place(x=1150, y=570)
    bek = tk.Button(root, text='-', padx=22, pady=20, font='Calibri 20')
    bek.place(x=1150, y=700)
    bb = tk.Button(root, text='/', padx=22, pady=20, font='Calibri 20')
    bb.place(x=1150, y=930)
    bç = tk.Button(root, text='*', padx=20, pady=20, font='Calibri 20')
    bç.place(x=1150, y=820)

    e1.bind('<Key>', b3g)  # Bind the event to the function

c1.place(x=420, y=170)
bbir = Button(root, text='1.dereceden denklemler', bg='gray91', padx=300, pady=30, font='Calibri 30', command=b1c)
bbir.place(x=450, y=300)
biki = Button(root, text='2.dereceden denklemler', bg='gray91', padx=300, pady=30, font='Calibri 30')
biki.place(x=450, y=500)

def hesapla(a, b, c):
    return a * x ** 2 + b * x + c

m = 0.5 * x + x ** 2 + 5

plt.plot(x, hesapla(0, 0, 0))
plt.grid()

plt.show()

root.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.