self.text_box = tk.Text(self.master, width=100, height=50, font=('宋体', 15)) self.text_box.pack(side=tk.LEFT, padx=10, pady=5)text设置位置及大小
时间: 2023-06-18 15:07:05 浏览: 31
这段代码中,我们创建了一个文本框(Text),设置它的宽度为100,高度为50,字体为宋体,字号为15。然后使用pack()方法将文本框放置在左侧,设置左侧与其他控件的间距(padx)为10,顶部与其他控件的间距(pady)为5。这样就可以在界面上显示一个大小为100x50、字号为15的文本框,并且放置在左侧。
相关问题
如何设置Text文本内容?:self.text_box = tk.Text(root, width=30, height=10, font=('宋体', 20))
你可以使用 `insert` 方法向 `Text` 组件中添加文本内容,如下所示:
```python
self.text_box.insert(tk.END, "这是要添加的文本内容")
```
其中,`tk.END` 表示文本框中的末尾位置。如果你想要设置文本框中的内容,可以使用 `delete` 方法清空原有的内容,再使用 `insert` 方法添加新的内容,如下所示:
```python
self.text_box.delete('1.0', tk.END) # 清空文本框中的内容
self.text_box.insert(tk.END, "这是要设置的新内容")
```
其中,`'1.0'` 表示文本框中的第一行第一列位置。
# 创建右侧面板 self.panel_right = tk.Frame(master, bg='white') self.panel_right.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) # 创建文本框 text_frame = tk.Frame(self.panel_right) text_frame.pack(side=tk.TOP, fill=tk.BOTH, expand=True) self.result_text = tk.Text(text_frame) self.result_text.pack(side=tk.TOP, fill=tk.BOTH, expand=True)在這段代碼中再添加一個右下方添加一個文本框
可以在 `self.panel_right` 中再添加一个 `text_frame2`,然后在 `text_frame2` 中创建一个 `tk.Text` 对象。以下是示例代码:
```
# 创建右侧面板
self.panel_right = tk.Frame(master, bg='white')
self.panel_right.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# 创建文本框1
text_frame = tk.Frame(self.panel_right)
text_frame.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
self.result_text = tk.Text(text_frame)
self.result_text.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
# 创建文本框2
text_frame2 = tk.Frame(self.panel_right)
text_frame2.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
self.result_text2 = tk.Text(text_frame2)
self.result_text2.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
```
这样就在右侧面板的底部添加了一个新的文本框 `self.result_text2`。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)