import tkinter as tk class Three_layout(): def __init__(self, top): self.top = top self.frame = tk.Frame(self.top, bg='Gold', width=1100, height=234, highlightthickness=2, highlightbackground='Black') self.frame.place(x=0, y=670) self.canvas = tk.Canvas(self.frame, width=1077, height=226, bg='Gold') self.canvas.pack(side='left', fill='both', expand=True) self.scrollbar = tk.Scrollbar(self.frame, command=self.canvas.yview, orient=tk.VERTICAL) self.scrollbar.pack(side='right', fill='y') self.canvas.configure(yscrollcommand=self.scrollbar.set) self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) self.button_frame = tk.Frame(self.canvas, bg='Yellow') self.canvas.create_window((0, 0), window=self.button_frame, anchor="nw") self.Content() self.Text_Add() def Content(self): self.text = tk.Text(self.button_frame, width=1077) self.text.pack(side='left', fill='both', expand=True) self.text.insert('end', f'\n') self.text.config(state='disabled') self.button_frame.update_idletasks() self.canvas.configure(scrollregion=self.canvas.bbox("all")) def Text_Add(self): self.text.insert('end', "Hello, world!")为什么没有显示hello,world
时间: 2024-01-11 16:04:25 浏览: 172
在你的代码中,你在`Text_Add()`方法中调用了`insert()`方法,但是你没有将`state`设置为`normal`,这会导致你无法在`Text`控件中插入文本。所以你需要在调用`insert()`方法之前,将`state`设置为`normal`,插入完文本之后再将`state`设置为`disabled`,以保持`Text`控件的只读状态。修改后的代码如下:
```
import tkinter as tk
class Three_layout():
def __init__(self, top):
self.top = top
self.frame = tk.Frame(self.top, bg='Gold', width=1100,
height=234, highlightthickness=2, highlightbackground='Black')
self.frame.place(x=0, y=670)
self.canvas = tk.Canvas(self.frame, width=1077, height=226, bg='Gold')
self.canvas.pack(side='left', fill='both', expand=True)
self.scrollbar = tk.Scrollbar(self.frame, command=self.canvas.yview, orient=tk.VERTICAL)
self.scrollbar.pack(side='right', fill='y')
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.button_frame = tk.Frame(self.canvas, bg='Yellow')
self.canvas.create_window((0, 0), window=self.button_frame, anchor="nw")
self.Content()
self.Text_Add()
def Content(self):
self.text = tk.Text(self.button_frame, width=1077)
self.text.pack(side='left', fill='both', expand=True)
self.text.insert('end', f'\n')
self.text.config(state='disabled')
self.button_frame.update_idletasks()
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
def Text_Add(self):
self.text.config(state='normal')
self.text.insert('end', "Hello, world!")
self.text.config(state='disabled')
root = tk.Tk()
app = Three_layout(root)
root.mainloop()
```
在修改后的代码中,我将`Text_Add()`方法中的`self.text.config(state='normal')`添加了进去,以便在调用`insert()`方法之前将`state`设置为正常状态。
阅读全文