I need to clear window before I load a new map. But menu buttons have to stay. I was trying self.frame.destroy(), but it kill my menu buttons too. Basically I need to clear class Sokoban, but this class is not in class Window. Problem is in def map1(self): and def map2(self):
import tkinter
from tkinter import *
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
self.master.title('Sokoban')
self.pack(fill = BOTH, expand = 1)
menu = Menu(self.master)
self.master.config(menu=menu)
file = Menu(menu)
file.add_command(label='Mapa1', command = self.map1)
menu.add_cascade(label='Mapy', menu=file)
edit = Menu(menu)
edit.add_command(label='Mapa2', command = self.map2)
menu.add_cascade(label='Edit', menu=edit)
def map1(self):
self.frame.forget()
Sokoban('map1.txt')
def map2(self):
self.frame.forget()
Sokoban('map2.txt')
class Sokoban:
def __init__(self, subor):
t = open(subor, 'r')
self.pole = [None] * 8
self.box = []
for a in range(8):
self.pole[a] = list(t.readline().strip())
for b in range(len(self.pole[a])):
if self.pole[a][b] == '$':
self.player = Player(a,b)
self.pole[a][b] = '.'
if self.pole[a][b] == '#':
self.box.append(Box(a,b))
self.pole[a][b] = '.'
t.close()
self.game = self.g = tkinter.Canvas(bg = 'white', width = 650, height = 240,
cursor = 'cross')
self.g.pack()
self.farba = [['gray'],['khaki'],['#FF4500'],['yellow'],['brown']]
stena = '*'
policko = '.'
player = '$'
box = '#'
ciel = '+'
## self.newButt = self.g.create_rectangle(511, 0, 650, 45, fill = 'red')
## self.newButtText = self.g.create_text(580.5, 22.5, text = 'New Game',
## font = 'arial 19 italic bold',
## fill = 'yellow')
## self.map1Butt = self.g.create_rectangle(511, 46, 650, 86, fill = 'red')
## self.map1ButtText = self.g.create_text(580.5, 66, text = 'Map - 1',
## font = 'arial 19 italic bold',
## fill = 'yellow')
## self.map2Butt = self.g.create_rectangle(511, 87, 650, 127, fill = 'red')
## self.map2ButtText = self.g.create_text(580.5, 109, text = 'Map - 2',
## font = 'arial 19 italic bold',
## fill = 'yellow')
## self.map3Butt = self.g.create_rectangle(511, 128, 650, 168, fill = 'red')
## self.map3ButtText = self.g.create_text(580.5, 148, text = 'Map - 3',
## font = 'arial 19 italic bold',
## fill = 'yellow')
self.kresli()
self.g.bind_all('<Up>', self.up)
self.g.bind_all('<Down>', self.down)
self.g.bind_all('<Left>', self.left)
self.g.bind_all('<Right>', self.right)
## self.g.bind('<1>', self.Buttons)
def up(self, e):
self.move(-1, 0)
def down(self, e):
self.move(1, 0)
def left(self, e):
self.move(0, -1)
def right(self, e):
self.move(0, 1)
## def newGame(self):
## self.game.pack_forget()
## Sokoban()
## def map1(self):
## self.game.pack_forget()
## Sokoban('map1.txt')
##
## def map2(self):
## self.game.pack_forget()
## Sokoban('map2.txt')
## def Buttons(self, e):
## if '<1>' and self.newButt:
## self.newGame()
## if '<1>' and self.map1Butt:
## self.map1()
##
## if '<1>' and self.map2Butt:
## self.map2()
def kresli(self):
for a in range(8):
for b in range(17):
x,y = b*30, a*30
f = self.farba['*.+'.index(self.pole[a][b])]
self.g.create_rectangle(x,y,x+30,y+30,fill=f)
self.player.kresli(self.g, self.farba[3])
for box in self.box:
box.kresli(self.g, self.farba[4])
def setBox(self, r, s):
for a in range(len(self.box)):
if self.box[a].r==r and self.box[a].s==s:
return a
return -1
def move(self, dr, ds):
r = self.player.r + dr
s = self.player.s + ds
if r<0 or r>=8 or s<0 or s>=510 or self.pole[r][s] == '*':
return
c = self.setBox(r, s)
if c >= 0:
rr = r + dr
ss = s + ds
if rr < 0 or rr>=8 or ss<0 or ss>=510 or self.pole[rr][ss] == '*':
return
if self.setBox(rr, ss) >= 0:
return
self.box[c].move(dr, ds)
self.player.move(dr, ds)
for c in self.box:
if self.pole[c.r][c.s] != '+':
return
class Player:
def __init__(self, r, s):
self.r = r
self.s = s
def kresli(self, g, farba):
self.g = g
x,y = self.s*30, self.r*30
self.plr = tkinter.PhotoImage(file='player.png')
self.id1 = g.create_image(x+15, y+15, image = self.plr)
def move(self, dr, ds):
self.r = self.r + dr
self.s = self.s + ds
self.g.move(self.id1, ds*30, dr*30)
class Box:
def __init__(self, r, s):
self.r = r
self.s = s
def kresli(self, g, farba):
self.g = g
x,y = self.s*30, self.r*30
self.bx = tkinter.PhotoImage(file='box.png')
self.id2 = g.create_image(x+15, y+15, image = self.bx)
def move(self, dr, ds):
self.r = self.r + dr
self.s = self.s + ds
self.g.move(self.id2, ds*30, dr*30)
root = Tk()
root.geometry('510x240')
app = Window(root)
root.mainloop()
Sokoban()
That not-working wall of code is very hard to work with so I will try to answer your menu buttons have to stay part with an example code.
Basically you create two frames, one holds widgets going to be cleared and second one holds other widgets plus first frame.
import tkinter as tk
root = tk.Tk()
root.geometry("300x300")
frame1 = tk.Frame(root)
frame2 = tk.Frame(frame1)
frame1.pack()
frame2.pack()
menubar = tk.Menu(frame1)
menubar.add_command(label="Bring Back!", command=frame2.pack)
menubar.add_command(label="Quit!", command=root.destroy)
tk.Button(frame2, text="Forget only frame2", command=frame2.pack_forget).pack()
tk.Label(frame2, text="Label on frame2").pack()
root.config(menu=menubar)
root.mainloop()
Related
I'm trying to run a GUI on a separate process from my "main" program while still being able to retrieve values from the GUI in the "main" module.
Right now I have a simple GUI setup with a simple function on the "main" module to get several values triggered by a button press.
I have instantiated both classes in my "main" module but after I start the GUI process, press the 'start' button to trigger my retrieve & print function I receive the following error:
NameError: name 'start' is not defined
"main" module:
#############################
#### Importing Libraries ####
#############################
from GUI import *
#############################
#### Creating Main Class ####
#############################
class Round_Sling_Main:
def sling_start(self):
current_length = start.get_length()
current_speed = start.get_speed()
current_color = start.get_color()
print(current_length, current_speed, current_color)
if __name__ == "__main__":
round_sling = Round_Sling_Main()
start = GUI(round_sling, 0, 0, "").start()
GUI module:
#############################
#### Importing Libraries ####
#############################
import tkinter as tk
from tkinter import ttk
import sys
from multiprocessing import Process
############################
#### Creating Front GUI ####
############################
class GUI(Process):
def __init__(self, round_sling, length, speed, color):
super(GUI, self).__init__()
self.round_sling = round_sling
self.length = length
self.speed = speed
self.color = color
self.create_GUI()
def retrieve_length(self, length):
self.length = length
def retrieve_speed(self, speed):
self.speed = speed
def retrieve_color(self, event = None):
self.color = event.widget.get()
def get_length(self):
return self.length
def get_speed(self):
return self.speed
def get_color(self):
return self.color
def close(self):
sys.exit()
def create_GUI(self):
while(1):
front = tk.Tk()
front.title ("Round Sling Machine")
front.geometry ('800x600')
#front.config (cursor = 'none')
#front.attributes ("-fullscreen", True)
# Color Selection Label
Color_Label = tk.Label (front, text = 'Select a Sling Color', font = 'Verdana 12')
Color_Label.place(x = 300, y = 0)
# Material Selection Label
Material_Label = tk.Label (front, text = 'Select a Sling Material', font = 'Verdana 12')
Material_Label.place(x = 300, y = 80)
# Length Entry Label
Length_Label = tk.Label (front, text = 'Select Desired Sling Length (inches)', font = 'Verdana 12')
Length_Label.place(x = 240,y = 160)
# Speed Factor Label
Speed_Label = tk.Label (front, text = 'Enter Motor Speed Factor (0.0 - 1.0)', font = 'Verdana 8')
Speed_Label.place(x = 25, y = 35)
# Color Selection Combo Box
ColorVar = tk.StringVar()
SlingColor = ttk.Combobox(front, width = 27, textvariable = ColorVar)
SlingColor['values'] = ('Purple',
'Green',
'Yellow',
'Tan',
'Red',
'White',
'Blue',
'Orange 1',
'Orange 2',
'Orange 3',
'Orange 4',
'Orange 5',
'Orange 6')
SlingColor.state(["readonly"])
SlingColor.place(x = 300, y = 30)
SlingColor.bind("<<ComboboxSelected>>", self.retrieve_color)
# Material Selection Combo Box
MaterialVar = tk.StringVar()
MaterialSel = ttk.Combobox(front, width = 27, textvariable = MaterialVar)
MaterialSel['values'] = ('Nylon', 'Polyester', 'Carbon Fiber')
MaterialSel.state(["readonly"])
MaterialSel.place(x = 300, y = 110)
# Sling Length Slider
SlingLength = tk.Scale(front, from_ = 0, to = 100, length = 600, orient=tk.HORIZONTAL, command = self.retrieve_length)
SlingLength.place(x = 105, y = 190)
# Motor Speed Factor Slider
MotorSpeed = tk.Scale(front, from_ = 0, to = 1.0, resolution = 0.1, length = 200, orient=tk.HORIZONTAL, command = self.retrieve_speed)
MotorSpeed.place(x = 25, y = 50)
startBtn = tk.Button(front, text = 'Start', font = 'Verdana 12', command = self.round_sling.sling_start)
startBtn.place(x = 250, y = 250)
closeBtn = tk.Button(front, text = 'Close', font = 'Verdana 12', command = self.close)
closeBtn.place(x = 450, y = 250)
front.mainloop()
############################
##### End of Front GUI #####
############################
I have a HangMan game created using a class. I want to delete all the child widgets with a single deletion of the parent window.
My game shows a difficulty window, which is being destroyed after the user selects a difficulty and the main game window appears; which is destroyed too after the user completes the game and then a replay window appears; which is destroyed too after the user clicks yes and a new instance of the difficulty pop-up appears again and the whole thing happens intetely until the user says no.
My code looks like this:
class HangMan:
font = ('Comic Sans MS', 20, 'bold')
theme = 'Dark'
allWords = []
stages = None
title = None
def __init__(self):
self.tries = None
self.pWord = None
self.word = None
self.used = []
self.char = None
def setup(self, path, stges, title="HangMan"):
with open(path, 'r') as file:
self.allWords = file.read().split()
del file
self.stages = stges
self.title = title
def play(self):
def setTheme(theme, place):
if theme == 'Dark':
b = 'black'
f = 'white'
else:
b = 'white'
f = 'black'
if place == 'askDiff':
self.diffWindow.config(bg = b)
self.diffFrame['bg'] = b
i = [self.diffLabel, self.db1, self.db2, self.db3]
elif place == 'Main':
self.window.config(bg = b)
inputEntry['insertbackground'] = f
for i in [frame1, frame2]:
i['bg'] = b
i = [wordLabel, themeButton, ph, inputEntry, triesLabel, usageLabel, logLabel]
elif place == 'askReplay':
self.replayWindow.config(bg = b)
self.reFrame['bg'] = b
i = [self.reLabel, self.reb1, self.reb2]
for j in i:
j['bg'] = b
j['fg'] = f
def changeTheme():
if self.theme == 'Dark':
self.theme = 'Light'
else:
self.theme = 'Dark'
themeButton['text'] = self.theme
setTheme(self.theme, 'Main')
def replay():
self.replayWindow.destroy()
self.__init__()
self.play()
def askReplay(event):
#Destroying the running game
self.window.destroy()
#Creating the replay Window
self.replayWindow = Tk()
self.replayWindow.title('Re-Experience?')
self.reLabel = Label(master = self.replayWindow, font = self.font, text = "Wanna replay?\nSwear it'll be a different word...")
self.reLabel.pack()
self.reFrame = Frame(master = self.replayWindow)
self.reFrame.pack()
self.reb1 = Button(master = self.reFrame, font = self.font, text = 'Yup', command = replay)
self.reb1.pack(side = 'left')
self.reb2 = Button(master = self.reFrame, font = self.font, text = 'Later.', command = lambda: self.replayWindow.destroy())
self.reb2.pack(side = 'left')
setTheme(self.theme, 'askReplay')
self.replayWindow.mainloop()
def askDiff():
def returnStage(num):
nonlocal x
x = num
self.diffWindow.destroy()
self.diffWindow = Tk()
self.diffWindow.title('Difficulty')
self.diffLabel = Label(master = self.diffWindow, font = self.font, text = 'Choose your level of difficulty: ')
self.diffLabel.pack()
self.diffFrame = Frame(master = self.diffWindow)
self.diffFrame.pack()
x=None
self.db1 = Button(master = self.diffFrame, font = self.font, text = self.stages[0]['stage'], command = lambda: returnStage(0))
self.db1.pack(side = 'left')
self.db2 = Button(master = self.diffFrame, font = self.font, text = self.stages[1]['stage'], command = lambda: returnStage(1))
self.db2.pack(side = 'left')
self.db3 = Button(master = self.diffFrame, font = self.font, text = self.stages[2]['stage'], command = lambda: returnStage(2))
self.db3.pack(side = 'left')
setTheme(self.theme, 'askDiff')
self.diffWindow.mainloop()
return x
def setDiff(i):
from random import choice
wordLenRange = (self.stages[i]['wordLen']['min'], self.stages[i]['wordLen']['max'])
possibleWords = []
for j in self.allWords:
if len(j) in range(wordLenRange[0], wordLenRange[1] + 1):
possibleWords.append(j)
self.word = list(choice(possibleWords).lower())
self.pWord = ''.join(self.word)
self.allWords.remove(self.pWord)
self.tries = self.stages[i]['tries']
def isValid(string):
if len(string)==1 and string.isalpha():
return True
else:
return False
def isUsed(string):
if string in self.used or string in wordLabel['text']:
return True
else:
return False
def isRight(string):
if string in self.word:
return True
else:
return False
def updateLog(x):
if x == 'invalid input':
logLabel['text'] = 'Enter a valid single alphabet'
elif x == 'in word':
logLabel['text'] = 'This letter is revealed'
elif x == 'in used':
logLabel['text'] = 'You have already used this letter'
elif x == 'right':
logLabel['text'] = 'Good Guess...'
elif x == 'wrong':
logLabel['text'] = 'You missed it.'
elif x == 'win':
logLabel['text'] += '\nYou Win...Press any key...'
else:
logLabel['text'] += '\nYou Lost...The word is revealed.\nPress any key...'
def updateWord():
txt = list(wordLabel['text'])
while self.char in self.word:
index = self.word.index(self.char)
txt[index] = self.char
self.word[index] = '*'
txt = ''.join(txt)
wordLabel['text'] = txt
def updateTries():
self.tries -= 1
triesLabel['text'] = '{} Oops left'.format(self.tries)
def updateUsage():
self.used.append(self.char)
usageLabel['text'] = 'Used Letters: {}'.format(', '.join(self.used))
def processGame(event):
self.char = inputEntry.get().strip()
if isValid(self.char):
self.char = self.char.lower()
if isUsed(self.char):
if self.char in wordLabel['text']:
updateLog('in word')
else:
updateLog('in used')
else:
if isRight(self.char):
updateWord()
updateLog('right')
else:
updateTries()
updateUsage()
updateLog('wrong')
else:
updateLog('invalid input')
inputEntry.delete(0, 'end')
if '*' not in wordLabel['text']:
updateLog('win')
inputEntry.bind('<Key>', askReplay)
if self.tries == 0:
updateLog('lost')
wordLabel['text'] = self.pWord
inputEntry.bind('<Key>', askReplay)
## GUI Design
from tkinter import Tk, Label, Entry, Button, Frame
#Diffficulty PopUp
diff = askDiff()
setDiff(diff)
# Main Window
self.window = Tk()
self.window.title(self.title)
# Frames and Buttons
frame1 = Frame(master = self.window)
frame1.pack(fill = 'x')
wordLabel = Label(master = frame1, font = self.font, text = '*'*len(self.word))
wordLabel.pack()
themeButton = Button(master = frame1, font = ("Comic Sans MS", 12), text = self.theme, command = changeTheme)
themeButton.pack(side = 'right')
frame2 = Frame(master = self.window)
frame2.pack()
ph = Label(master = frame2, font = self.font, text = 'Guess a Letter: ')
ph.pack(side = 'left')
inputEntry = Entry(master = frame2, font = self.font, width = 5)
inputEntry.focus_set()
inputEntry.pack(side = 'left')
triesLabel = Label(master = frame2, font = self.font, text = '{} Oops left'.format(self.tries))
triesLabel.pack(side = 'left')
usageLabel = Label(master = self.window, font = self.font, text = 'Used Letters: -')
usageLabel.pack()
logLabel = Label(master = self.window, font = self.font, text = "-:Log:-")
logLabel.pack()
setTheme(self.theme, 'Main')
#Event Handler and MainLoop
self.window.bind('<Return>', processGame)
self.window.mainloop()
path = "words.txt"
stages = ({'stage': 'Cool', 'tries':10, 'wordLen':{'min':3, 'max':5}}, {'stage': 'Engaging', 'tries':6, 'wordLen':{'min':6, 'max':8}}, {'stage': 'Intense', 'tries': 4, 'wordLen': {'min': 9, 'max': 15}})
game1 = HangMan()
game1.setup(path, stages)
game1.play()
At 3 places, I'm destroying the parent window - in the askReplay() fucntion of the class.play() function, in the returnStage() function of the askDiff() function in the class.play() function & in the replay() function of the class.play() function. I tried self.window.destroy(), but it is only destroying the root window and all its child widgets are still occupying the memory (I know this because I tried to print one of the child widgets after the window was destroyed). How can I do this?
This would be a problem if an obsessive player plays my game for a
thousand times, creating more than 7000 trash widgets in the memory.
One implementation would be to have the top-level object track its widgets via a list, then have a .close() method that looks something like:
def close(self):
for widget in self.widgets:
widget.destroy()
self.destroy()
In the ShowMoneyButton class, I want the activation function to only activate if self.button_activated is False, and I set it as False when the SongButton class's activation function is triggered, yet it's not registering. It stays True. Is there a way I can make it so the ShowMoneyButton class's activation function is executed only if the SongButton class's activation function is executed, except for the first time?
import tkinter as tk, random, subprocess, time, os, datetime, pprint
class Song:
def __init__(self):
self.files = []
self.path = "/users/eliasrahoui/desktop/songy"
for filename in os.listdir(self.path):
self.files.append(self.path + "/" + filename)
class Money_Value:
def __init__(self):
with open("money_counter.txt", "r") as f:
a = f.read()
a = int(a)
self.current_value = a
class Clock:
def __init__(self, root):
money = Money_Value()
self.money = money.current_value
self.root = root
self.label = tk.Label(self.root, text = "$" + str(self.money), foreground = "green", font = ('calibri', 100, 'bold'))
self.label.pack()
def countup(self, *args):
root = self.root
money_per_hour = 1000
self.money += 1
self.label['text'] = "$" + str(self.money)
self.root_id = root.after(money_per_hour, self.countup)
def stop(self):
root = self.root
root.after_cancel(self.root_id)
with open("money_counter.txt", "w") as f:
f.write(str(self.money))
def save(self):
with open("money_counter.txt", "w") as f:
f.write(str(self.money))
self.root.destroy()
class Button:
all_buttons = {}
windows = []
return_code = []
button_activated = False
def __init__(self, root, text, command):
self.text = text
button = tk.Button(root, text = text, command = command)
self.all_buttons[self.text] = button
def remove(self, button):
button.pack_forget()
def show_button(self, button):
button.pack(side ="bottom")
class SongButton(Button):
def __init__(self, root, clock):
self.root = root
self.root_clock = clock
super().__init__(self.root, text = "stop song", command = self.activation)
def activation(self):
windows = self.windows
for i in windows:
i.destroy()
code = self.return_code[-1]
code.kill()
self.remove(self.all_buttons["stop song"])
self.root_clock.countup()
self.button_activated = False
class ShowMoneyButton(Button):
def __init__(self, root, clock):
self.root = root
super().__init__(self.root, "show me the money", self.activation)
song = Song()
self.songs = song.files
money_show_button = self.all_buttons["show me the money"]
self.show_button(money_show_button)
self.pic = tk.PhotoImage(file = "/users/eliasrahoui/desktop/IMG_7465.png")
#self.button_activated = False
self.label = tk.Label(self.root, text = "")
self.root_clock = clock
def activation(self):
if self.button_activated == False:
self.root_clock.stop()
num = Money_Value()
money = num.current_value
self.label.config(text =f'Money {money}')
self.label.pack()
random_song = random.choice(self.songs)
a = SongButton(self.root, self.root_clock)
return_cod = subprocess.Popen(["afplay", random_song])
self.return_code.append(return_cod)
self.show_button(self.all_buttons["stop song"])
self.button_activated = True
for _ in range(30):
root = self.root
window = tk.Toplevel(self.root)
self.windows.append(window)
window.minsize(100, 100)
window_label = tk.Label(window, image = self.pic)
window_label.pack()
ws = root.winfo_screenwidth() # width of the screen
hs = root.winfo_screenheight() # height of the screen
x = random.randint(0, ws)
y = random.randint(0, hs)
w = 200
h = 200
window.geometry('%dx%d+%d+%d' % (w, h, x, y))
class Timer(Button):
def __init__(self, root, start_time, name, counter = False):
self.count_down_start_time = start_time
self.root = root
self.name = name
self.counter = counter
self.tmp_counter = counter
super().__init__(self.root, text = name, command = self.timer_window)
self.show_button(self.all_buttons[self.name])
self.timer_id = None
self.window_label = None
self.orignial = start_time
self.window = None
self.current_question = 1
self.questions_answered = {}
self.times = [self.orignial]
self.positive = False
self.min_max = {}
def timer_window(self):
self.window = tk.Toplevel(self.root)
self.window.geometry('%dx%d+%d+%d' % (300, 200, 500, 300))
self.window_label = tk.Label(self.window, text = "", font = ('calibri', 100, 'bold'))
self.counter_label = tk.Label(self.window, text = f'Current Question: {self.current_question}')
self.window_label.pack()
self.timer()
def timer(self):
mins, secs = divmod(self.count_down_start_time, 60)
mins = abs(mins)
secs = abs(secs)
timerr = '{:02d}:{:02d}'.format(mins, secs)
if self.count_down_start_time == -1:
self.positive = True
self.window_label["foreground"] = "red"
if self.positive:
self.count_down_start_time += 1
else:
self.count_down_start_time -= 1
self.window_label["text"] = timerr
self.window.protocol("WM_DELETE_WINDOW", self.callback)
if self.counter == True:
button = tk.Button(self.window, text = "Question Done!", command = self.counter_func)
button.pack(side = "bottom")
button_report = tk.Button(self.window, text = "Report", command = self.report)
self.counter = False
self.counter_label.pack()
button_report.pack(side = "bottom")
self.timer_id = self.window.after(1000, self.timer)
def callback(self):
self.window.after_cancel(self.timer_id)
self.window.destroy()
self.count_down_start_time = self.orignial
if self.tmp_counter == True:
self.counter = True
self.current_question = 1
self.questions_answered = {}
self.positive = False
self.times = [self.orignial]
def counter_func(self):
self.questions_answered[self.current_question] = str(datetime.timedelta(seconds = self.times[-1] - self.count_down_start_time))
self.times.append(self.count_down_start_time)
self.current_question += 1
self.counter_label["text"] = f'Current Question: {self.current_question}'
def report(self):
vals = self.times_min_max()
new_window = tk.Toplevel(self.window)
new_window.minsize(300,300)
a = self.questions_answered[1]
data_pretty = pprint.pformat(self.questions_answered)
label = tk.Label(new_window, text = data_pretty)
label.pack()
mx = {f'Maximum time took per question: {vals[0]} question numbers': vals[2]}
mn = {f'Minimum time took per question: {vals[1]} question numbers': vals[-1]}
mx_label = tk.Label(new_window, text = mx, font = ('calibri', 14, 'bold'))
mn_label = tk.Label(new_window, text = mn, font = ('calibri', 14, 'bold'))
mn_label.pack(side = "bottom")
mx_label.pack(side = "bottom")
def times_min_max(self):
f = {}
big = []
small = []
for i in range(1, len(self.questions_answered) + 1):
val = self.questions_answered[i]
if ":" in val:
q = val.split(":")
secs = int(q[-1])
mins = q[0]
secs = secs/60
secs = str(secs)
secs = secs.split(".")
secs = secs[-1]
final = mins + "." + secs
final = (float(final)) * 60
final = round(final)
f[i] = final
else:
f[i] = int(val)
max_val = max(f, key = f.get) #max(f.items(), key=operator.itemgetter(1))[0]
min_val = min(f, key=f.get)#min(f.keys(), key=(lambda key: f[key]))#min(f.items(), key = operator.intemgetter(1))[0]
min_val = f[min_val]
max_val = f[max_val]
for i in f.keys():
if f[i] == max_val:
big.append(i)
if f[i] == min_val:
small.append(i)
return (self.questions_answered[big[0]], self.questions_answered[small[0]], big, small)
main function:
import clocky_classy as cm
import tkinter as tk
root = tk.Tk()
root.minsize(400,200)
my_clock = cm.Clock(root)
money_button = cm.ShowMoneyButton(root, my_clock)
root.protocol("WM_DELETE_WINDOW", my_clock.save)
timer = cm.Timer(root, 120, "2 min timer")
timer = cm.Timer(root, 1800, "30 min timer", counter = True)
my_clock.countup()
root.mainloop()
I am currently working on a chess program in python (Tkinter), I have got a board to generate with buttons so that I can click on pieces, I planned to use a canvas for each of the chess pieces and then move them around. Is there any way to make it so I can click through the canvas?
try:
from Tkinter import *
except:
from tkinter import *
class grid_piece():
def __init__(self, canvas, row, column, colour_boolean, size):
colour_1 = '#934904'
colour_2 = '#fce2c9'
self.row = row
self.column = column
grid_piece = Button(canvas, height = size // 100,
width = size // 50,
command=lambda: my_board.button_press([row, column]))
grid_piece.grid(row=row, column = column)
if colour_boolean:
grid_piece.configure(bg = colour_1, fg = colour_1)
else:
grid_piece.configure(bg = colour_2, fg = colour_2)
class pawn():
def __init__(self, size, canvas, position):
self.pawn_canvas = Canvas(canvas, width = size // 8, height = size // 8
)
self.pawn_canvas.grid(row = position[0], column = position[1])
class Board():
def __init__(self, master, size):
self.canvas = Canvas(master, width=size, height=size)
self.canvas.pack()
self.size = size
def generate_board(self):
colour_boolean = True
self.board_array = []
for column in range(8):
column_pieces = []
colour_boolean = not colour_boolean
for row in range(8):
colour_boolean = not colour_boolean
piece = grid_piece(self.canvas, row, column, colour_boolean, self.size)
column_pieces.append(piece)
self.board_array.append(column_pieces)
def display_pieces(self):
white_pieces = []
black_pieces = []
for x in range(8):
Pawn = pawn(self.size, self.canvas, [1, x])
white_pieces.append(Pawn)
def button_press(self, position):
print(position)
def onclick(*args):
print(*args)
root = Tk()
my_board = Board(root, 600)
my_board.generate_board()
my_board.display_pieces()
root.mainloop()
Here is an image of my board, the white squares are blank canvases that I will turn into objects once I create the pieces.
I'd like to make a program which takes an integer from user and makes as many entries in a Tkinkter window. Than from those entries take data and make a graph base on them. But when i turn on my program evereything is fine only a graph is wrong. It shows me an empty graph without any data on it, with no title and no "labels names". Here is my code. Please help. I'm using Python 2.7.5, PyDev for Eclipse
# -*- coding: utf-8 -*-
import matplotlib.pyplot as mp
import Tkinter as T, sys
def end():
sys.exit()
def check():
z = e.get()
try:
z = int(z)
e.config(bg = 'green')
e.after(1000, lambda: e.config(bg = 'white'))
x = []
y = []
global x1, y1
x1 = []
y1 = []
l2 = T.Label(main, text = 'X',bg = 'yellow')
l2.pack()
for i in range(0,z):
x.append(T.Entry(main, justify = 'center'))
x[i].pack()
x1.append(x[i].get())
l3 = T.Label(main, text = 'Y', bg = '#3366ff')
l3.pack()
for i in range(0,z):
y.append(T.Entry(main, justify = 'center'))
y[i].pack()
y1.append(y[i].get())
except:
e.config(bg = 'red')
e.after(1000, lambda: e.config(bg = 'white'))
return x1,y1
def graph():
mp.ion()
mp.plot(x1,y1)
mp.title('Wykres')
mp.xlabel('x')
mp.ylabel('y')
mp.draw()
#====================================================================#
y1 = []
x1 = []
z = 0
main = T.Tk()
main.title('GRAPH')
main.geometry('600x600')
main.config(bg = "#3366ff")
e = T.Entry(main,justify = 'center')
l = T.Label(main,text = 'Podaj liczbę parametrów N =',bg = '#3366ff')
b1 = T.Button(main, text = 'OK', command = check)
b2 = T.Button(main, text = 'Rysuj', command = graph)
b = T.Button(main,text = 'Zakończ', command = end)
l.pack()
e.pack()
b1.pack()
b2.pack()
b.pack()
main.mainloop()
GB: x1, x2 have only empty strings. You have to get values from Entry in graph() function when Entry have some value - and remember to convert it from string to integer.
PL: x1, x2 zawierają tylko puste stringi. Musisz pobrać wartości z Entry w funkcji graph() kiedy Entry już zawierają jakieś wartości - i pamiętaj przekonwertować je z napisów na liczby
working code / działający kod
# -*- coding: utf-8 -*-
import matplotlib.pyplot as mp
import Tkinter as T, sys
class Application():
def __init__(self, root):
self.root = root
self.x1 = []
self.y2 = []
self.z = 0
self.root.title('GRAPH')
self.root.geometry('600x600')
self.root.config(bg="#3366ff")
self.e = T.Entry(self.root, justify='center')
self.l = T.Label(self.root, text='Podaj liczbę parametrów N =', bg='#3366ff')
self.b1 = T.Button(self.root, text='OK', command=self.check)
self.b2 = T.Button(self.root, text='Rysuj', command=self.graph)
self.b = T.Button(self.root, text='Zakończ', command=self.end)
self.l.pack()
self.e.pack()
self.b1.pack()
self.b2.pack()
self.b.pack()
#------------------------------------------------------------
def run(self):
self.root.mainloop()
#------------------------------------------------------------
def end(self):
sys.exit()
#------------------------------------------------------------
def check(self):
try:
self.entry_x = []
self.entry_y = []
self.z = int(self.e.get())
self.e.config(bg='green')
self.e.after(1000, lambda: self.e.config(bg='white'))
self.l2 = T.Label(self.root, text='X', bg='yellow')
self.l2.pack()
for i in range(self.z):
self.entry_x.append(T.Entry(self.root, justify='center'))
self.entry_x[-1].pack()
self.l3 = T.Label(self.root, text='Y', bg='#3366ff')
self.l3.pack()
for i in range(self.z):
self.entry_y.append(T.Entry(self.root, justify='center'))
self.entry_y[-1].pack()
except:
self.e.config(bg='red')
self.e.after(1000, lambda: self.e.config(bg='white'))
#------------------------------------------------------------
def graph(self):
self.x1 = []
self.y1 = []
for i in range(len(self.entry_x)):
self.x1.append(float(self.entry_x[i].get()))
for i in range(len(self.entry_y)):
self.y1.append(float(self.entry_y[i].get()))
mp.ion()
mp.plot(self.x1, self.y1)
mp.title('Wykres')
mp.xlabel('x')
mp.ylabel('y')
mp.draw()
#====================================================================#
Application(T.Tk()).run()