gpt4 book ai didi

python - 有没有办法扩大Python中的GUI框?

转载 作者:行者123 更新时间:2023-12-01 00:58:51 25 4
gpt4 key购买 nike

我正在尝试制作一个简单的句子生成器。代码可以工作,但我只是想知道是否有任何方法可以扩展 GUI 输入框?

它是这样的:

enter image description here

如有任何帮助,我们将不胜感激。

import tkinter as tk
from tkinter import Entry
import random
window = tk.Tk()

def movingVerb():
verbs = ["goes to", "walks through", "runs through"]
moveVerb = random.choice(verbs)
return moveVerb
def anObject():
anObject = objectEntry.get()
def Generate():
UserName = nameEntry.get()
moveVerb = movingVerb()
place = placeEntry.get()
noun = objectEntry.get()
sentence = UserName + " " + moveVerb + " a " + place + " and finds a " + noun + " ."
result.delete(0, tk.END)
result.insert(0, sentence)

UserNameLabel = tk.Label(window, text="Enter a name: ")
nameEntry = tk.Entry(window)
anObjectLabel = tk.Label(window,text="Enter an object: ")
objectEntry = tk.Entry(window)
placeLabel = tk.Label(window, text="Enter a place: ")
placeEntry = tk.Entry(window)
button = tk.Button(window, text="Generate story", command=Generate)
result = tk.Entry(window)

UserNameLabel.pack()
nameEntry.pack()
anObjectLabel.pack()
objectEntry.pack()
placeLabel.pack()
placeEntry.pack()
button.pack()
result.pack()
window.mainloop()

最佳答案

如果您想在调整窗口大小时展开小部件,则可以在 pack() 中以不同的组合使用 expandfill 。一切都取决于您想要扩展哪个元素以及向哪个方向扩展。

.pack(fill='x')
.pack(fill='y')
.pack(fill='both')
.pack(expand=True, fill='x')
.pack(expand=True, fill='y')
.pack(expand=True, fill='both')

enter image description here

<小时/>

如果您想调整窗口大小以查看 Entry 中的所有新文本,则可以使用 widget.configure(width=...)。条目使用字符数作为宽度。

您可以使用句子的长度来更改宽度

result.configure(width=len(sentence))

或保持宽度至少 20 个字符

length = max(20, len(sentence))
result.configure(width=length)
<小时/>

完整代码

import tkinter as tk
import random

# --- functions ---

verbs = ["goes to", "walks through", "runs through"]

def generate():
user_name = name_entry.get()
move_verb = random.choice(verbs)
place = place_entry.get()
noun = object_entry.get()
sentence = '{} {} a {} and finds a {}.'.format(user_name, move_verb, place, noun)
result.delete(0, 'end')
result.insert(0, sentence)
length = max(20, len(sentence))
result.configure(width=length)

# --- main ---

window = tk.Tk()

user_name_label = tk.Label(window, text="Enter a name: ")
name_entry = tk.Entry(window)
object_label = tk.Label(window,text="Enter an object: ")
object_entry = tk.Entry(window)
place_label = tk.Label(window, text="Enter a place: ")
place_entry = tk.Entry(window)
button = tk.Button(window, text="Generate story", command=generate)
result = tk.Entry(window)

user_name_label.pack(expand=True, fill='both')
name_entry.pack(fill='both')
object_label.pack(expand=True, fill='both')
object_entry.pack(fill='both')
place_label.pack(expand=True, fill='both')
place_entry.pack(fill='both')
button.pack(expand=True, fill='both')
result.pack(fill='both')

window.mainloop()

关于python - 有没有办法扩大Python中的GUI框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55991538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com