如何添加tkinter滚动条与网格?



我是Python编程新手。我几周前开始学这门语言。我正在创建一个"温度转换器"。应用程序使用"tkinter"。我试图添加一个滚动条的应用程序,但无法这样做。如果我写错了什么,请原谅。请让我知道我的错误,我一定会改正的。我以后不会再犯同样的错误了。

非常感谢您的宝贵时间。下面是我的代码:
import tkinter as tk
from tkinter.messagebox import showerror
from tkinter import *
window = tk.Tk()
window.title("Temperature Converter")
window.geometry('420x400')
window.option_add('*Font', '25')
window.option_add('*Font', 'Tahoma')
window.configure(bg='#f8f8f8')
window.resizable(0, 0)
def fahrenheit_to_celsius():
fahrenheit = fahrenheit_to_celsius_entry_temperature.get()
if fahrenheit == '':
showerror(title='Empty Field Error', message="Field is empty. Please input number.")
else:
try:
celsius = (5 / 9) * (float(fahrenheit) - 32)
fahrenheit_to_celsius_label_result["text"] = f"{round(celsius, 2)} N{DEGREE CELSIUS}"
except:
showerror(title='Input Error', message="Invalid input. Please input only number.")
fahrenheit_to_celsius_label_heading = tk.Label(text="Fahrenheit to Celsius:", bg='#f8f8f8', fg='Black')
# Create the Fahrenheit entry frame with an Entry widget and label in it
fahrenheit_to_celsius_frame_entry = tk.Frame(master=window)
fahrenheit_to_celsius_entry_temperature = tk.Entry(master=fahrenheit_to_celsius_frame_entry, width=10)
fahrenheit_to_celsius_label_temperature = tk.Label(master=fahrenheit_to_celsius_frame_entry, text="N{DEGREE FAHRENHEIT}")
def fahrenheit_to_celsius_clear():
fahrenheit_to_celsius_entry_temperature.delete(0, tk.END)
fahrenheit_to_celsius_label_result.config(text="N{DEGREE CELSIUS}")
fahrenheit_to_celsius_button_clear = tk.Button(window, text="Clear", command=fahrenheit_to_celsius_clear, bg='#c82333', fg='White')
fahrenheit_to_celsius_label_heading.grid(row=0, column=0, columnspan=3, sticky="w")
fahrenheit_to_celsius_entry_temperature.grid(row=1, column=0, sticky="ew")
fahrenheit_to_celsius_label_temperature.grid(row=1, column=1, sticky="ew")
fahrenheit_to_celsius_button_clear.grid(row=1, column=2, sticky="w")
fahrenheit_to_celsius_button_convert = tk.Button(master=window, text="Convert", command=fahrenheit_to_celsius, bg='#218838', fg='White')
fahrenheit_to_celsius_label_result = tk.Label(master=window, text="N{DEGREE CELSIUS}")
fahrenheit_to_celsius_label_heading.grid(row=0, padx=10, pady=10)
fahrenheit_to_celsius_frame_entry.grid(row=1, column=0, padx=12, sticky="ew")
fahrenheit_to_celsius_button_convert.grid(row=1, column=1, padx=0, sticky="ew")
fahrenheit_to_celsius_button_clear.grid(row=1, column=2, padx=(5,0), sticky="ew")
fahrenheit_to_celsius_label_result.grid(row=1, column=3, padx=0, sticky="w")

添加滚动条

滚动条不能用于tkinter框架,但可以用于画布。

程序
  • 把你所有的小部件放在一个更小的画布
  • 配置canvas scrollregion
  • 为画布添加滚动条

例子
base = tk.Canvas(window)
base.pack(fill=tk.BOTH, side=tk.LEFT, expand=True)
# change scrollregion as changes are made to the canvas
base.bind('<Configure>', lambda e: base.configure(scrollregion=base.bbox(tk.ALL)))
# add scrollbar for the canvas
scroll = tk.Scrollbar(window, command=base.yview)
scroll.pack(side=tk.LEFT, fill=tk.Y)
base.config(yscrollcommand=scroll.set)
# sample widgets
for i in range(2, 20):
btn = tk.Button(base, text=f"Button {i}")
base.create_window(200, i*30, window=btn)
对于你的代码,你需要:
  • 按照上面的说明创建一个可滚动的画布
  • 不直接使用grid,而是对每个部件使用canvas.create_window(x, y, window)

最新更新