Python 使用tkinter使用外部文件中的字创建网格

Python 使用tkinter使用外部文件中的字创建网格,python,file,random,tkinter,Python,File,Random,Tkinter,好的,这段代码可以很好地显示网格,但是它的编程方式非常低效,我知道有一种更快的方法可以做到这一点,但我似乎无法得到它 另外,为了使这个网格是随机的,我需要单词被随机分配,而不是总是在同一个地方。我试图将其添加到列表中,然后使用“random.shuffle”,但一旦我这样做,我就无法从列表中提取单词,然后将它们放入网格中 我已经把程序精简到我能工作的部分。程序的另一部分使网格切换到相同的单词,但处于不同的随机位置 import random from tkinter import * roo

好的,这段代码可以很好地显示网格,但是它的编程方式非常低效,我知道有一种更快的方法可以做到这一点,但我似乎无法得到它

另外,为了使这个网格是随机的,我需要单词被随机分配,而不是总是在同一个地方。我试图将其添加到列表中,然后使用“random.shuffle”,但一旦我这样做,我就无法从列表中提取单词,然后将它们放入网格中

我已经把程序精简到我能工作的部分。程序的另一部分使网格切换到相同的单词,但处于不同的随机位置

import random

from tkinter import *

root=Tk()

word_list=[]

with open("easy_words.txt", "r") as f:

    word_1=f.read(6)
    word_2=f.read(6)
    word_3=f.read(6)
    word_4=f.read(6)
    word_5=f.read(6)
    word_6=f.read(6)
    word_7=f.read(6)
    word_8=f.read(6)
    word_9=f.read(6)

def easy_grid():

    w=Label(root, text=(word_1), fg='black').grid(row=1, column=1)
    w=Label(root, text=(word_2), fg='black').grid(row=1, column=2)
    w=Label(root, text=(word_3), fg='black').grid(row=1, column=3)
    w=Label(root, text=(word_4), fg='black').grid(row=2, column=1)
    w=Label(root, text=(word_5), fg='black').grid(row=2, column=2)
    w=Label(root, text=(word_6), fg='black').grid(row=2, column=3)
    w=Label(root, text=(word_7), fg='black').grid(row=3, column=1)
    w=Label(root, text=(word_8), fg='black').grid(row=3, column=2)
    w=Label(root, text=(word_9), fg='black').grid(row=3, column=3)

def menu():

    b=Button(root,text='Easy', command=easy_grid()).grid(row=4, column=1)
    b=Button(root,text='Hard', command=print('Hard game')).grid(row=4, column=3)
    b=Button(root, text='Close', command=root.destroy).grid(row=4, column=2)


menu()

我建议进行以下改进和修复:

  • 在文件中使用一些分隔符存储单词,例如空格或逗号。然后执行
    words=f.read().split(“”)
    将所有单词作为一个列表
  • 如果要将该列表随机化,请使用
    random.shuffle(单词)
  • 使用两个嵌套循环(或单个循环和一些数学)填充网格
  • b=按钮(…)。网格(…)
    grid
    的结果分配给
    b
    ,而不是按钮;标签也一样
  • command=easy\u grid()
    将执行函数一次,并将结果(
    None
    再次)绑定到按钮,而不是函数本身。改用
    command=easy\u grid
    (无
    ()
  • 您需要调用
    root.mainloop()
    来显示帧
大概是这样的:

def easy_grid():
    with open("easy_words.txt", "r") as f:
        easy_words = f.read().split()
    random.shuffle(easy_words)
    for i, w in enumerate(easy_words):
        Label(root, text=w, fg='black').grid(row=i//3, column=i%3)

def menu():
    Button(root,text='Easy', command=easy_grid).grid(row=4, column=0)
    Button(root, text='Close', command=root.destroy).grid(row=4, column=1)
    Button(root,text='Hard', command=lambda:print('Hard game')).grid(row=4, column=2)

root = Tk()
menu()
root.mainloop()

有一种更简单的读取文件的方法:使用循环

word_list=[]
with open("easy_words.txt", "r") as f:
    for line in f:
        word_list.append(line)
这将读取文件中的每一行,并将它们添加到列表的末尾。你以后可以洗牌

您的easy grid方法还可以通过循环进行改进:

def easy_grid():
    for i in range(9):
        w=Label(root, text=(word_list[i]), fg='black').grid(row=(i%3)+1, column=(i/3)+1)
重复操作9次(由
范围(9)
给出)。使用变量
i
确定要进入的行/列(行的模数,列的除法)

如果您希望它是随机的,只需在调用
easy\u grid