我想滚动标签中的内容。我是个乞讨者。请帮帮我。我像below.But一样做了,它不工作。这是我在网上找到的。但它不起作用。我从.txt文件中提取了一些数据,需要在标签中显示。标签不足以显示它们全部。所以我需要滚动标签中的内容。
from tkinter import ttk
import tkinter as tk
from tkinter import *
from PIL import Image, ImageTk
window=tk.Tk()
im = Image.open("landscape2.png")
tkimage = ImageTk.PhotoImage(im)
tab_control = ttk.Notebook(window)
tab5 = ttk.Frame(tab_control)
tab_control.add(tab5, text='History')
tab_control.pack(expand=1, fill='both')
his_lbl = tk.Label(tab5, image=tkimage)
his_lbl.place(relwidth = 1, relheight = 1)
his_frame = tk.Frame(tab5, bg='#80c1ff',bd=5)
his_frame.place(relx = 0.3, rely = 0.1, relheight=0.1, relwidth=0.50,
anchor= 'n')
button = tk.Button(his_frame, bg = 'white', command = lambda:
get_weather(his_entry.get()))
button.place(relx = 0.7, relheight = 1, relwidth = 0.3)
his_entry = tk.Entry(his_frame, font =('Courier', 18))
his_entry.place(relheight = 1, relwidth = 0.65)
canvas = Canvas(tab5, bg="white")
canvas.place(relx = 0.3, rely = 0.25, relheight = 0.6, relwidth = 0.50,
anchor='n')
lst = []
y = 0
label = Label(canvas,anchor='w', font=("Courier", 20),
compound=RIGHT,bg='white',bd=4, justify="left")
label.place(relwidth=1,relheight=1)
canvas.create_window(0, y, window=label, anchor=NW)
y += 60
scrollbar = Scrollbar(canvas, orient=VERTICAL, command=canvas.yview)
scrollbar.place(relx=1, rely=0, relheight=1, anchor=NE)
canvas.config(yscrollcommand=scrollbar.set, scrollregion=(0, 0, 0, y))
def get_weather(history):
file=open((history+".txt"),("r"))
a=(file.read())
label['text'] = a
window.mainloop()发布于 2019-10-09 19:25:58
在我看来,你试图用一个Canvas来做什么,仅仅是为了让一个Label可滚动,这是一种夸张的做法。您可以改用Text小部件,并将其状态设置为disabled,以防止用户编辑其内容。
import tkinter as tk
root = tk.Tk()
# create text widget
text = tk.Text(root, background=root.cget('background'), relief='flat', height=10)
# insert text
content = "Long text to display\n" * 20
text.insert('1.0', content)
# disable text widget to prevent editing
text.configure(state='disabled')
# scrolling
scroll = tk.Scrollbar(root, orient='vertical', command=text.yview)
text.configure(yscrollcommand=scroll.set)
scroll.pack(side='right', fill='y')
text.pack(side='left', fill='both', expand=True)
root.mainloop()https://stackoverflow.com/questions/58302057
复制相似问题