jeudi 26 novembre 2015

Using tkinter to create a grid using words from an external file

Okay, this code can display the grid fine, but the way it is programmed is extremely inefficient and I know there is a quicker way of doing this but I can't seem to get it. Also, in order for this grid to be random I need the words to be randomly allocated rather than always being in the same place and I'm trying to add it to a list and then use 'random.shuffle' but once I've done that I can't extract the words from the list and then put them into the grid. I've stripped the program down to only the parts which I can get working. Another part of the program is to make the grid switch to the same words but in different, random positions. I'm relatively new to Python so this probably sounds very basic so, apologies.

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()




Aucun commentaire:

Enregistrer un commentaire