I'm building Minesweeper in python with Tkinter and built the adjustable board size with a for loop using grid to position each value on the window.
Unfortunately I cannot find a way to now reposition the entire grid i.e., I would like to be able to move the game board within the window to place other widgets around it (currently it's auto positioned is the top left corner).
Part of my code is:
import tkinter as tk
import configparser
class MainWindow:
def __init__(self, master):
"""
"""
self.master = master
self.config = configparser.SafeConfigParser()
self.master.geometry("600x600")
self.master.resizable(width=False, height=False)
self.buttons = []
for row in range(0, 9):
self.buttons.append([])
for col in range(0, 9):
self.button = tk.Button(text=" ", font=("Georgia", "10", "bold"), width=4, height=2,
background="lightsteelblue", relief="raised")
self.button.bind("<Button-3>")
self.button.grid(row=row, column=col, sticky=tk.N+tk.W+tk.S+tk.E)
self.buttons[row].append(self.button)
if __name__ == '__main__':
root = tk.Tk()
root.configure(background="cornflowerblue")
app = MainWindow(root)
root.mainloop()
This part of the code returns the following:
Which shows the issue I'm having. I would like the ability to move the board within the window (for example, by using place) so that I can enlarge the frame and add features around the hopefully centralised board.
You should move the buttons into a frame. You can them move the entire group of widgets at once. Even better, create a class specifically for the grid. It would look something like this:
class GridFrame(tk.Frame):
def __init__(self, parent, rows=10, columns=10, **kwargs):
super().__init__(parent, **kwargs)
self.buttons = []
self.grid_rowconfigure([*range(rows)], weight=1, uniform="row")
self.grid_columnconfigure([*range(columns)], weight=1, uniform="column")
for row in range(rows):
self.buttons.append([])
for col in range(columns):
button = tk.Button(
self,
text=" ",
font=("Georgia", "10", "bold"),
width=4,
height=2,
background="lightsteelblue",
relief="raised",
)
button.grid(row=row, column=col, sticky=tk.N + tk.W + tk.S + tk.E)
self.buttons[row].append(button)
You can use this just like you use any other widget. For example:
class MainWindow:
def __init__(self, master):
""" """
self.master = master
self.config = configparser.SafeConfigParser()
self.master.geometry("600x600")
self.board = GridFrame(self.master, rows=10, columns=10, borderwidth=0)
self.board.grid(row=1, column=1, sticky="nsew")
Related
I try to adopt Tkinter, treeview doesn't resize with window, with verry little modification.
import tkinter as tk
from tkinter import ttk
import random
class App():
def __init__(self):
self.root = tk.Tk()
self.frame = tk.Frame(self.root)
self.frame.pack(expand=True, fill=tk.BOTH)
self.tree = ttk.Treeview(self.frame, show="headings")
self.tree.pack(expand=True, )
self.frameBT = tk.LabelFrame(self.root,text='Buttons')
self.frameBT.pack(expand=True, fill=tk.X)
self.button = ttk.Button(self.frameBT, text="Fill", command=self.fill)
self.button.pack(side=tk.BOTTOM,expand=True)
self.fill()
self.root.mainloop()
def fill(self):
if self.has_data():
self.tree.delete(*self.tree.get_children())
i = random.randrange(1,10)
self.tree["columns"]=tuple([str(i) for i in range(i)])
for col in self.tree['columns']:
self.tree.heading(col, text="Column {}".format(col), anchor=tk.CENTER)
self.tree.column(col, anchor=tk.CENTER)
j = random.randrange(10)
for j in range(j):
self.tree.insert("", "end", values = tuple([k for k in range(i)]))
def has_data(self):
has_tree = self.tree.get_children()
return True if has_tree else False
App()
Currently, when I stretch the window verticaly, I got
My question is, how to keep that 'Button' at the verry bottom, while the bottom of treeview frame move up and down when I resize the main window.
You need to:
make self.tree to fill all available space by setting fill=tk.BOTH:
self.tree.pack(expand=True, fill=tk.BOTH) # added fill=tk.BOTH
disable expand of self.frameBT by removing expand=True:
self.frameBT.pack(fill=tk.X) # removed expand=True
I have the current code below for some basic parameter entry into an AI assignment. It is just there to st the starting parameters and display the outpit of the different algorithms implemented, however the box that contains the output will not resize? I think I am doing something wrong with maybe the parent-child structure but I can't figure out what.
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_widgets()
def create_widgets(self):
self.mainframe= tk.Frame(master=self, width=768, height=576)
self.mainframe.pack(fill=tk.BOTH, expand=1)
self.xsizelabel = tk.Label(self.mainframe, text="Size (X)")
self.xsizelabel.pack(side="top")
self.xsize = tk.Entry(self.mainframe)
self.xsize.insert(0, 2)
self.xsize.pack(side="top")
self.ysizelabel = tk.Label(self.mainframe, text="Size (Y)")
self.ysizelabel.pack(side="top")
self.ysize = tk.Entry(self.mainframe)
self.ysize.insert(0, 1)
self.ysize.pack(side="top")
self.xstartlabel = tk.Label(self.mainframe, text="Starting Position (X)")
self.xstartlabel.pack(side="top")
self.xStart = tk.Entry(self.mainframe)
self.xStart.insert(0, 0)
self.xStart.pack(side="top")
self.ystartlabel = tk.Label(self.mainframe, text="Starting Position (Y)")
self.ystartlabel.pack(side="top")
self.yStart = tk.Entry(self.mainframe)
self.yStart.insert(0, 0)
self.yStart.pack(side="top")
self.outputstartlabel = tk.Label(self.mainframe, text="Output")
self.outputstartlabel.pack(side="top")
self.separator = tk.Frame(master=self.mainframe, width=768, height=576, bd=1)
self.separator.pack(fill=tk.BOTH, padx=5, pady=5)
self.output = tk.Scrollbar(self.separator)
self.output.pack(side=tk.RIGHT, fill=tk.Y)
self.listbox = tk.Listbox(self.separator, yscrollcommand=self.output.set)
self.listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
self.run_button = tk.Button(self.mainframe)
self.run_button["text"] = "Run with these settings"
self.run_button["command"] = self.runAlgorithm
self.run_button.pack(side="top")
self.quit = tk.Button(self.mainframe, text="QUIT", fg="red",
command=self.master.destroy)
self.quit.pack(side="bottom")
but the resulting window looks like this:
default
expanded
nothing expands when I expand the window, dispite setting the autofill and expand options. what am I doing wrong?
I can't run your program because you didn't present the whole thing. I see that you have set the fill and expand options on self.mainframe, but you didn't set those options in the constructor. Therefore the base window, which contains self.mainframe, will not expand to fill its available space. You need to make all the parent windows expandable, because when you drag the edges of the main window you are acting on the top level frame.
A simple quiz game
I got this code and I need scrollbars, I tried to search how to add it on stackoverflow (ScrolledWindow with tix...) but I still can't get something that works properly. Could someone help me?
from tkinter import *
from random import randint
root = Tk()
root.title("Quiz")
root.geometry("400x300")
class Window:
def __init__(self, question, answer):
self.text = [question, answer]
self.createLabel()
# self.createText()
self.createEntry()
self.createButton()
def retrieve_input(self):
# inputValue = self.textBox.get("1.0", "end-1c")
# print(inputValue)
if self.mystring.get() == self.text[1]:
print("Esatto. è " + self.text[1])
self.left['text'] = "Esatto"
def createLabel(self):
self.labelframe = LabelFrame(root, text="Domanda:")
self.labelframe.pack(fill="both", expand="yes")
self.left = Label(self.labelframe, text=self.text[0])
self.left.pack()
def createText(self):
self.textBox = Text(height=1)
self.textBox.pack()
def createEntry(self):
self.mystring = StringVar()
self.myentry = Entry(root, textvariable=self.mystring).pack()
def createButton(self):
self.but = Button(text="Click", command=self.retrieve_input)
self.but.pack()
for i in range(10):
one = randint(1, 10)
two = randint(1, 10)
Window("Quanto fa " + str(one) + "+" + str(two) + "?", str(one + two))
root.mainloop()
output
With ScrolledFrame it can look like this
I renamed Window into Question because it makes more sense
I use self.question and self.answer instead of self.text = [question, answer] to make it more readable.
I put classes and functions before root = tk.Tk() to make it more readable.
I use import tkinter as tk instead of from tkinter import * to make it more readable.
Question gets inner frame from ScrolledFrame and use as parent for LabelFrame. Other widgets use labelframe as parent.
BTW: you had entry = Entry(..).pack() which assign None to entry because pack()/grid()/place() returns None. I put pack() in next line and now I can get text directly from Entry (without StringVar)
Code
import tkinter as tk
from random import randint
# --- classes ---
class ScrolledFrame(tk.Frame):
def __init__(self, parent, vertical=True, horizontal=False):
super().__init__(parent)
# canvas for inner frame
self._canvas = tk.Canvas(self)
self._canvas.grid(row=0, column=0, sticky='news') # changed
# create right scrollbar and connect to canvas Y
self._vertical_bar = tk.Scrollbar(self, orient='vertical', command=self._canvas.yview)
if vertical:
self._vertical_bar.grid(row=0, column=1, sticky='ns')
self._canvas.configure(yscrollcommand=self._vertical_bar.set)
# create bottom scrollbar and connect to canvas X
self._horizontal_bar = tk.Scrollbar(self, orient='horizontal', command=self._canvas.xview)
if horizontal:
self._horizontal_bar.grid(row=1, column=0, sticky='we')
self._canvas.configure(xscrollcommand=self._horizontal_bar.set)
# inner frame for widgets
self.inner = tk.Frame(self._canvas, bg='red')
self._window = self._canvas.create_window((0, 0), window=self.inner, anchor='nw')
# autoresize inner frame
self.columnconfigure(0, weight=1) # changed
self.rowconfigure(0, weight=1) # changed
# resize when configure changed
self.inner.bind('<Configure>', self.resize)
self._canvas.bind('<Configure>', self.frame_width)
def frame_width(self, event):
# resize inner frame to canvas size
canvas_width = event.width
self._canvas.itemconfig(self._window, width = canvas_width)
def resize(self, event=None):
self._canvas.configure(scrollregion=self._canvas.bbox('all'))
class Question:
def __init__(self, parent, question, answer):
self.parent = parent
self.question = question
self.answer = answer
self.create_widgets()
def get_input(self):
value = self.entry.get()
print('value:', value)
if value == self.answer:
print("Esatto. è " + self.answer)
self.label['text'] = "Esatto"
def create_widgets(self):
self.labelframe = tk.LabelFrame(self.parent, text="Domanda:")
self.labelframe.pack(fill="both", expand=True)
self.label = tk.Label(self.labelframe, text=self.question)
self.label.pack(expand=True, fill='both')
self.entry = tk.Entry(self.labelframe)
self.entry.pack()
self.button = tk.Button(self.labelframe, text="Click", command=self.get_input)
self.button.pack()
# --- main ---
root = tk.Tk()
root.title("Quiz")
root.geometry("400x300")
window = ScrolledFrame(root)
window.pack(expand=True, fill='both')
for i in range(10):
one = randint(1, 10)
two = randint(1, 10)
Question(window.inner, "Quanto fa {} + {} ?".format(one, two), str(one + two))
root.mainloop()
Tkinter experts, I'm having trouble getting a Canvas to scroll. This is my second GUI, and I've done something similar before, so I don't know what I'm doing wrong. I'd appreciate any help you can offer.
Here's a minimal version of what I'm trying to do. I'm using python 3.4.3 on Windows 10.
import tkinter as tk
import tkinter.font as tk_font
import tkinter.ttk as ttk
import random
def get_string_var(parent, value=''):
var = tk.StringVar(parent)
var.set(value)
return var
class SummaryFrame(ttk.Frame):
def __init__(self, parent, **kwargs):
ttk.Frame.__init__(self, parent, **kwargs)
var_names = ['label_'+str(num) for num in range(1, 20)]
self.vars = {}
for name in var_names:
self.vars[name] = get_string_var(self)
self._add_summary_labels(self, self.vars, 1)
#staticmethod
def _add_summary_labels(frame, vars, start_row):
current_row = start_row
for name in vars:
tk.Label(frame, text=name, anchor=tk.N+tk.W).grid(row=current_row, column=0, sticky=tk.N+tk.S+tk.W+tk.E)
text_label = tk.Label(frame, wraplength=200, textvariable=vars[name], anchor=tk.N+tk.W, justify=tk.LEFT)
text_label.grid(row=current_row, column=1, sticky=tk.W)
current_row += 1
def set_summary_fields(self, info):
for name in info:
if name in self.vars:
self.vars[name].set(info[name])
class OuterFrame(ttk.Frame):
def __init__(self, parent, **kwargs):
ttk.Frame.__init__(self, parent, **kwargs)
self.canvas = tk.Canvas(self)
scrollbar = ttk.Scrollbar(self, orient=tk.VERTICAL, command=self.canvas.yview)
self.canvas.configure(yscrollcommand=scrollbar.set)
self.summary = SummaryFrame(self.canvas)
self.summary.pack(fill=tk.BOTH, expand=1)
self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
scrollbar.pack(side=tk.LEFT, fill=tk.Y, expand=1)
label_text = {}
for i in range(1, 20):
label_text['label_'+str(i)] = "information " * random.randint(1, 20)
self.set_fields(label_text)
def set_fields(self, info):
self.summary.set_summary_fields(info)
self.canvas.configure(scrollregion=(1, 1, self.summary.winfo_width(), self.summary.winfo_height()))
if __name__ == "__main__":
root = tk.Tk()
frame = OuterFrame(root)
frame.pack(fill=tk.BOTH, expand=1)
root.mainloop()
The scrollbar should change when the contents of the inner SummaryFrame expands, but doesn't. It remains grayed out and inoperable. What am I doing wrong?
Short answer: you are configuring the scrollregion to be one pixel by one pixel.
You are configuring the scrollregion based on the width and height of the self.summary, but you are doing this before the window has a chance to be mapped to the screen. The width and height, therefore, are both one.
You need to wait for the window to be drawn before computing the width and height of the window.
I'm trying to put together a window that displays a bunch of labels generated from a dict. I'm having trouble getting the scrollbars to work properly. They won't stick to the sides of the frame when I resize the window, and I can't get the canvas to respond to the scroll command. I need the window to support a large number of labels.
from Tkinter import *
from math import floor
bits = {}
#the dict is then built
class Bitbox(Canvas):
def __init__(self, parent, bitdict, *args, **kwargs):
Canvas.__init__(self, parent, background="black")
self.bitdict = bitdict
self.parent = parent
self.lbllist = []
n=0
for i in bitdict.keys():
label = Label(self, text=i, bg='black', fg='green')
n += 1
label.grid(row = ((n-1)%30), column=int(floor((n-1)/30)))
self.lbllist.append(label)
def main():
root = Tk()
frame = Frame(root)
frame.grid(sticky=N+S+E+W)
bts = Bitbox(frame, bits)
bts.grid(row=0, column=0)
vbar = Scrollbar(frame, orient=VERTICAL)
vbar.grid(row=0, column=1, sticky=N+S)
vbar.config(command=bts.yview)
hbar = Scrollbar(frame, orient=HORIZONTAL)
hbar.grid(row=1, column=0, columnspan=2, sticky=W+E)
bts.config(xscrollcommand=hbar.set)
hbar.config(command=bts.xview)
bts.config(yscrollcommand=vbar.set)
bts.config(scrollregion=(0,0,500,1000))
root.mainloop()
Clearly I'm new at all this. It's entirely possible I have a fundamental misunderstanding of how these widgets interact. Any help is much appreciated.
to get the scrollbar to react to the mouse bind the mouse to the scrollbar like this:
def on_mousewheel(event):
bts.yview_scroll(-1*(event.delta/120), "units")
def main():
global bts
#your code...
root.bind_all("<MouseWheel>",on_mousewheel)