How can I explicitly set the terminal size in python? - python

I would like to explicitly set the terminal size. Is there a cross-platform way to do this?
I want to make a game, showing fixed-sized art.

I used Tkinter (Thanks Steven Rumbalski!), because I don't mind using GUI if it's cross-platform. Basically, I set a window geometry and the size of my "art" label:
from Tkinter import *
#Assign a variable to the shown image to be able to change it
displayed_image = "test.gif"
class App:
def __init__(self, master):
#Make a frame to enclose the art with a relief
self.topframe = Frame(master, borderwidth=5, pady=5, relief=RIDGE)
#Pack the frame to draw it
self.topframe.pack()
#This frame will contain the prompt
self.frame = Frame(master)
self.frame.pack()
photo = PhotoImage(file="../Art/"+displayed_image)
art = Label(self.topframe, width=1280, height=720, image=photo)
art.photo = photo
art.pack()
prompt = Entry(self.frame, width=1280)
prompt.pack()
root = Tk()
root.wm_geometry("%dx%d" % (1300, 780))
app = App(root)
root.mainloop()
root.destroy()

Related

How can I place a label of top of this frame? Tkinter

I am trying to place a label on top of a Frame, which is inside a 'Notebook' tab.
But when I run this code, the label always ends up in the center of the frame.
from tkinter import *
from tkinter import ttk
class Window:
def __init__(self,master):
self.master = master
master.title("Title")
master.resizable(1,1)
master.geometry('500x400')
self.load_UI()
def load_UI(self):
self.tabOptions = ttk.Notebook(self.master )
self.tab1 = Frame(self.tabOptions, padx=130, pady=80, bg='white')
self.tabOptions.add(self.tab1, text="Add Files")
self.tabOptions_AddFile()
self.tabOptions.pack()
def tabOptions_AddFile(self):
self.label = Label(self.tab1, text="Why is this in the center of the frame?")
self.label.grid(row=0, column=0)
root = Tk()
app = Window(root)
root.mainloop()
I tried to place the label using: pack(), grid(), place(). I also tried to place the label before adding the frame to the Notebook but it still looks the same :(
I am using python 3 btw.
This is because your Frame is padded in the line self.tab1 = Frame(self.tabOptions, padx=130, pady=80, bg='white'). Your Frame is here:
Just remove padx=130, pady=80 and all works. But to keep the size of tabOptions, replace
self.tabOptions.pack()
by
self.tabOptions.pack(fill=BOTH, expand=True)

How to shrink a frame in tkinter after removing contents?

Most of the topics I came across deals with how to not shrink the Frame with contents, but I'm interested in shrinking it back after the destruction of said contents. Here's an example:
import tkinter as tk
root = tk.Tk()
lbl1 = tk.Label(root, text='Hello!')
lbl1.pack()
frm = tk.Frame(root, bg='black')
frm.pack()
lbl3 = tk.Label(root, text='Bye!')
lbl3.pack()
lbl2 = tk.Label(frm, text='My name is Foo')
lbl2.pack()
So far I should see this in my window:
Hello!
My name is Foo
Bye!
That's great, but I want to keep the middle layer interchangeable and hidden based on needs. So if I destroy the lbl2 inside:
lbl2.destroy()
I want to see:
Hello!
Bye!
But what I see instead:
Hello!
███████
Bye!
I want to shrink frm back to basically non-existence because I want to keep the order of my main widgets intact. Ideally, I want to run frm.pack(fill=tk.BOTH, expand=True) so that my widgets inside can scale accordingly. However if this interferes with the shrinking, I can live without fill/expand.
I've tried the following:
pack_propagate(0): This actually doesn't expand the frame at all past pack().
Re-run frm.pack(): but this ruins the order of my main widgets.
.geometry(''): This only works on the root window - doesn't exist for Frames.
frm.config(height=0): Oddly, this doesn't seem to change anything at all.
frm.pack_forget(): From this answer, however it doesn't bring it back.
The only option it leaves me is using a grid manager, which works I suppose, but not exactly what I'm looking for... so I'm interested to know if there's another way to achieve this.
When you destroy the last widget within a frame, the frame size is no longer managed by pack or grid. Therefore, neither pack nor grid knows it is supposed to shrink the frame.
A simple workaround is to add a small 1 pixel by 1 pixel window in the frame so that pack still thinks it is responsible for the size of the frame.
Here's an example based off of the code in the question:
import tkinter as tk
root = tk.Tk()
lbl1 = tk.Label(root, text='Hello!')
lbl1.pack()
frm = tk.Frame(root, bg='black')
frm.pack()
lbl3 = tk.Label(root, text='Bye!')
lbl3.pack()
lbl2 = tk.Label(frm, text='My name is Foo')
lbl2.pack()
def delete_the_label():
lbl2.destroy()
if len(frm.winfo_children()) == 0:
tmp = tk.Frame(frm, width=1, height=1, borderwidth=0, highlightthickness=0)
tmp.pack()
root.update_idletasks()
tmp.destroy()
button = tk.Button(root, text="Delete the label", command=delete_the_label)
button.pack()
root.mainloop()
Question: Shrink a Frame after removing the last widget?
Bind to the <'Expose'> event and .configure(height=1) if no children.
Reference:
Expose
An Expose event is generated whenever all or part of a widget should be redrawn
import tkinter as tk
class App(tk.Tk):
def __init__(self):
super().__init__()
tk.Label(self, text='Hello!').pack()
self.frm = frm = tk.Frame(self, bg='black')
frm.pack()
tk.Label(self, text='Bye!').pack()
tk.Label(frm, text='My name is Foo').pack()
self.menubar = tk.Menu()
self.config(menu=self.menubar)
self.menubar.add_command(label='delete', command=self.do_destroy)
self.menubar.add_command(label='add', command=self.do_add)
frm.bind('<Expose>', self.on_expose)
def do_add(self):
tk.Label(self.frm, text='My name is Foo').pack()
def do_destroy(self):
w = self.frm
if w.children:
child = list(w.children).pop(0)
w.children[child].destroy()
def on_expose(self, event):
w = event.widget
if not w.children:
w.configure(height=1)
if __name__ == "__main__":
App().mainloop()
Question: Re-run frm.pack(): but this ruins the order of my main widgets.
frm.pack_forget(), however it doesn't bring it back.
Pack has the options before= and after. This allows to pack a widget relative to other widgets.
Reference:
-before
Use its master as the master for the slaves, and insert the slaves just before other in the packing order.
Example using before= and self.lbl3 as anchor. The Frame are removed using .pack_forget() if no children and get repacked at the same place in the packing order.
Note: I show only the relevant parts!
class App(tk.Tk):
def __init__(self):
...
self.frm = frm = tk.Frame(self, bg='black')
frm.pack()
self.lbl3 = tk.Label(self, text='Bye!')
self.lbl3.pack()
...
def on_add(self):
try:
self.frm.pack_info()
except:
self.frm.pack(before=self.lbl3, fill=tk.BOTH, expand=True)
tk.Label(self.frm, text='My name is Foo').pack()
def on_expose(self, event):
w = event.widget
if not w.children:
w.pack_forget()
Tested with Python: 3.5 - 'TclVersion': 8.6 'TkVersion': 8.6

Tkinter "couldn't open "pyimage1": no such file or directory" error when trying to open an image

I have been trying to add a background image to my Interface but I always get the error:
"couldn't open "pyimage1": no such file or directory"
Also I am pretty new to python.
Already tried multiple methods with tkinter and PIL aswell as tkinter's canvas which didn't work either
This is the whole programm:
import tkinter as tk
from PIL import Image, ImageTk
class MainMenu:
def __init__(self, master):
#creating Main Frame and Window
master.title("Interface")
#Image
image = Image.open(r"Images\matrix.jpg")
photo = ImageTk.PhotoImage(image)
self.background_image = tk.PhotoImage(file=photo)
self.background_label = tk.Label(image=self.background_image)
self.background_label.place(x=0,y=0)
self.background_label.img = self.background_image
#Creating Widgets
self.label1 = tk.Label(master, text="Please Enter the text you would
like encrypted: ")
self.entry1 = tk.Text(master, height=5, width=20)
self.button = tk.Button(master, text="Submit", command=self.Submit)
#Adding Widgets to Grid
self.label1.grid(row=0, column=0, padx=5, pady=5)
self.entry1.grid(row=1, column=0, padx=5, pady=5)
self.button.grid(columnspan=2, pady=10)
#Configuration of Widgets and Main window
master.configure(bg="black")
self.button.configure(bg="light blue")
self.label1.configure(bg="black", fg="light blue")
self.entry1.configure(bg="light blue")
def Submit(self):
print("You entered: " + self.entry1.get())
root = tk.Tk()
Mm = MainMenu(root)
root.mainloop()
The main problem would be within these lines I am guessing:
image = Image.open(r"Images\matrix.jpg")
photo = ImageTk.PhotoImage(image)
self.background_image = tk.PhotoImage(file=photo)
self.background_label = tk.Label(image=self.background_image)
self.background_label.place(x=0,y=0)
self.background_label.img = self.background_image
As you can see I am trying to make an Interface or GUI and everything is working fine except the background image.
Try this:
image = Image.open("Images\matrix.jpg")
photo = ImageTk.PhotoImage(image)
#self.background_image = tk.PhotoImage(file=photo) -- Not needed, delete
self.background_label = tk.Label(image=photo)
self.background_label.image = photo
self.background_label.place(x=0,y=0)
#self.background_label.img = self.background_image -- Also not needed, delete
As far as I can tell this simply means you have used 'tk.PhotoImage' twice on a variable.
For example:
item1_image = tk.Label()
image = tk.PhotoImage('image.png')
item1_image.configure(image=tk.PhotoImage(image))
When you're pulling these variables out of different places in a large file, it is hard to keep track of whether 'PhotoImage' is used. I typically use it as early as possible to avoid the image not appearing.

Buttons not appearing below image in python GUI

I am developing a GUI on Python and I have the following problem: I want to have the picture at the top of the window and buttons right underneath it. I am using the Tkinter module and whatever geometry I use (place, pack or grid) the buttons don't move. The are only displayed if I move the image using grid to row 1 (which is the second row), otherwise they don't appear at all. Here is the code I am using for now. For reference the picture has dimensions of 291x87 pixels.
import Tkinter
from Tkinter import *
def main():
window =Tk()
window.geometry("300x300")
window.title("Dienes Blocks Application")
window.iconbitmap(default='favicon.ico')
app = HomeScreen(window)
window.mainloop()
class HomeScreen(Frame):
def __init__(self, master):
Frame.__init__(self,master)
self.create_buttons()
self.sparx_head()
def sparx_head(self):
self.grid()
photo = Tkinter.PhotoImage(file="logosmall.gif")
sparx_header = Label(image=photo)
sparx_header.image = photo # keep a reference!
sparx_header.grid(column=0, row=0, columnspan=2, rowspan=2, sticky='NSEW')
def create_buttons(self):
self.grid()
#teacher button
teacher_button = Tkinter.Button(self, text="Teacher")
teacher_button.grid(column=0, row=10)
# student button
student_button = Tkinter.Button(self, text="Student")
student_button.grid(column=2, row=10)
# prototype button
prototype_button = Tkinter.Button(self, text="Prototype")
prototype_button.grid(column=1, row=10)
if __name__ == "__main__":
main()
You just need to use self in the image label:
sparx_header = Label(self,image=photo)
They were not having the same parent & that's why you were having this problem

python - TKinter - Can I send background to back?

I am new to programming and python so I thank you in advance for your patience.
I have created a class which creates a new window with a test image for it's background ("test1.gif").
I have also created a drop down menu within the same class that lets you choose between 3 countries.
I can comment off either the code that makes the background or the code that makes the menu and the other will display. However I want the menu to appear above the background. When I run the full code I only see the background.
I appreciate I am probably missing something very obvious here, if some one could please point out what that is.
Thanks for your time.
Dan
from Tkinter import *
import Tkinter as tk
class dropDown():
def __init__(self, master):
#background set_up
image1 = tk.PhotoImage(file="test1.gif")
w = image1.width()
h = image1.height()
root.geometry("%dx%d+0+0" % (w, h))
panel1 = tk.Label(root, image=image1)
panel1.pack(side='top', fill='both', expand='yes')
# save the panel's image from 'garbage collection'
panel1.image = image1
#Drop down menu
self.var = StringVar(master)
self.var.set('Alaska') # initial value
#Have not used countries list just pasted
self.option = OptionMenu(master, self.var, 'Alberta', 'Australia')
self.option.pack()
root = Tk()
root.title('drop down test')
dropDown(root)
mainloop()
(I think) Your problem is that you're restricting the size of the root window to be the size of the image (instead of the sizeof(image)+sizeof(menu)). This fixes the problem for me on OS-X.
import Tkinter as tk
class dropDown():
def __init__(self, master):
#background set_up
image1 = tk.PhotoImage(file="test.gif")
w = image1.width()
h = image1.height()
panel1 = tk.Label(root, image=image1)
panel1.pack(side='top', fill='both', expand='yes')
# save the panel's image from 'garbage collection'
panel1.image = image1
#Drop down menu
self.var = tk.StringVar(master)
self.var.set('Alaska') # initial value
#Have not used countries list just pasted
self.option = tk.OptionMenu(master, self.var, 'Alaska','Alberta', 'Australia')
self.option.pack()
geomstr="%dx%d+0+0" % (w, panel1.winfo_reqheight()+self.option.winfo_reqheight())
root.geometry(geomstr)
root = tk.Tk()
root.title('drop down test')
dropDown(root)
root.mainloop()
A side note, it would be very easy to have your dropDown inherit from Frame and then adjust the size of your Frame (Although it probably isn't necessary). Then you can move this widget anywhere you want to on the GUI.
EDIT
subclassing Frame:
import Tkinter as tk
class dropDown(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self,master)
#background set_up
image1 = tk.PhotoImage(file="test.gif")
panel1 = tk.Label(root, image=image1)
panel1.pack(side='top', fill='both', expand='yes')
# save the panel's image from 'garbage collection'
panel1.image = image1
#Drop down menu
self.var = tk.StringVar(master)
self.var.set('Alaska') # initial value
#Have not used countries list just pasted
self.option = tk.OptionMenu(master, self.var, 'Alaska','Alberta', 'Australia')
self.option.pack()
root = tk.Tk()
root.title('drop down test')
d=dropDown(root)
d.pack(side='top',fill='both',expand='yes')
root.mainloop()
Note that now Tkinter/TK take care of all the frame resizing for you. :)

Categories

Resources