如何使用 tkinter 随机化 python 中的按钮位置?
How to Randomize the button location in python using tkinter?
我正在尝试创建一个可点击的词云,我开始为其应用按钮逻辑。我能够使用正则表达式拆分文本文件中的单词并为每个单词创建按钮。但是每个按钮的位置都显示在列视图中。如下图所示:
tkinter 是否有任何功能可以随机化此位置并手动更改按钮,让我看起来像一个可点击的词云。
这是我的工作解决方案:
# import the 'tkinter' module
import tkinter
# import regular expression
import re
# create a new window
window = tkinter.Tk()
# set the window title
window.title("Commands")
# set the window icon
window.wm_icon
bitmap('Icon.ico')
# initially, the button hasn't been pressed
presses = 0
# a function that changes the text of the label to a location of the word clicked
def locate():
global presses
# add code to find the location of the word ---------------
# update the label text with the new value of 'presses' -----------
lbl.configure(text=presses)
# create a label to display a message
lbl = tkinter.Label(window, text=presses)
lbl.pack()
# looping through the number f words
# create a new button, assign a specific text to each button and provide an argument called 'command'
# which in this case calls a function called 'callback'
with open ('sample.txt','r') as f:
f_contents = f.read()
# Removing URLs
f_contents = re.sub(r'http\S+',"",f_contents)
# Removing all the punctuations
f_contents = re.sub(r'[^\w\s]', '', f_contents)
for word in f_contents.split():
btn = tkinter.Button(window, text=word, command=locate)
btn.pack()
# draw the window, and start the 'application'
window.mainloop()
提前致谢。
我不知道这是否是最好的方法,但是在从您的代码中删除 .pack() 以使用 .grid() 之后,我使用 numpy 生成随机数来随机放置按钮。
import numpy as np
with open('sample.txt', 'r') as f:
f_contents = f.read()
# Removing URLs
f_contents = re.sub(r'http\S+', "", f_contents)
# Removing all the punctuations
f_contents = re.sub(r'[^\w\s]', '', f_contents)
for word in f_contents.split():
btn = tkinter.Button(window, text=word, command=locate).grid(row=int(np.random.normal(len(f_contents)/2,1)),column=int(np.random.normal(len(f_contents)/2,1)))
我用了普通随机让它看起来像词云。
您可以使用 place
几何管理器将小部件放置在精确坐标处。您可以使用 random.randint
来计算随机坐标。如果您使用 canvas,您可以使用内置方法 find_overlapping
来确定是否有任何内容会与计算出的位置重叠。
这是一个简单的例子:
import tkinter as tk
import random
words = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
def place_label(canvas, label):
'''place a label on a canvas in a random, non-overlapping location'''
width = label.winfo_reqwidth()
height = label.winfo_reqheight()
tries = 0
while True and tries < 1000:
tries += 1 # failsafe, to prevent an infinite loop
x = random.randint(0, 200-width)
y = random.randint(0, 200-height)
items = canvas.find_overlapping(x, y, x+width, y+height)
if len(items) == 0:
canvas.create_window(x, y, window=label, anchor="nw")
break
root = tk.Tk()
canvas = tk.Canvas(root, width=200, height=200)
canvas.pack(fill="both", expand=True)
for word in words:
label = tk.Label(root, text=word)
place_label(canvas, label)
root.mainloop()
我正在尝试创建一个可点击的词云,我开始为其应用按钮逻辑。我能够使用正则表达式拆分文本文件中的单词并为每个单词创建按钮。但是每个按钮的位置都显示在列视图中。如下图所示:
tkinter 是否有任何功能可以随机化此位置并手动更改按钮,让我看起来像一个可点击的词云。
这是我的工作解决方案:
# import the 'tkinter' module
import tkinter
# import regular expression
import re
# create a new window
window = tkinter.Tk()
# set the window title
window.title("Commands")
# set the window icon
window.wm_icon
bitmap('Icon.ico')
# initially, the button hasn't been pressed
presses = 0
# a function that changes the text of the label to a location of the word clicked
def locate():
global presses
# add code to find the location of the word ---------------
# update the label text with the new value of 'presses' -----------
lbl.configure(text=presses)
# create a label to display a message
lbl = tkinter.Label(window, text=presses)
lbl.pack()
# looping through the number f words
# create a new button, assign a specific text to each button and provide an argument called 'command'
# which in this case calls a function called 'callback'
with open ('sample.txt','r') as f:
f_contents = f.read()
# Removing URLs
f_contents = re.sub(r'http\S+',"",f_contents)
# Removing all the punctuations
f_contents = re.sub(r'[^\w\s]', '', f_contents)
for word in f_contents.split():
btn = tkinter.Button(window, text=word, command=locate)
btn.pack()
# draw the window, and start the 'application'
window.mainloop()
提前致谢。
我不知道这是否是最好的方法,但是在从您的代码中删除 .pack() 以使用 .grid() 之后,我使用 numpy 生成随机数来随机放置按钮。
import numpy as np
with open('sample.txt', 'r') as f:
f_contents = f.read()
# Removing URLs
f_contents = re.sub(r'http\S+', "", f_contents)
# Removing all the punctuations
f_contents = re.sub(r'[^\w\s]', '', f_contents)
for word in f_contents.split():
btn = tkinter.Button(window, text=word, command=locate).grid(row=int(np.random.normal(len(f_contents)/2,1)),column=int(np.random.normal(len(f_contents)/2,1)))
我用了普通随机让它看起来像词云。
您可以使用 place
几何管理器将小部件放置在精确坐标处。您可以使用 random.randint
来计算随机坐标。如果您使用 canvas,您可以使用内置方法 find_overlapping
来确定是否有任何内容会与计算出的位置重叠。
这是一个简单的例子:
import tkinter as tk
import random
words = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
def place_label(canvas, label):
'''place a label on a canvas in a random, non-overlapping location'''
width = label.winfo_reqwidth()
height = label.winfo_reqheight()
tries = 0
while True and tries < 1000:
tries += 1 # failsafe, to prevent an infinite loop
x = random.randint(0, 200-width)
y = random.randint(0, 200-height)
items = canvas.find_overlapping(x, y, x+width, y+height)
if len(items) == 0:
canvas.create_window(x, y, window=label, anchor="nw")
break
root = tk.Tk()
canvas = tk.Canvas(root, width=200, height=200)
canvas.pack(fill="both", expand=True)
for word in words:
label = tk.Label(root, text=word)
place_label(canvas, label)
root.mainloop()