I am trying to create a simple input form for data entry, but I can't get the text input box to change the height, only the width changes when setting the size parameter.
Here is my code:
import PySimpleGUI as sg
def main():
# # create small form for Data Analysis entry
last_printer = sg.Input(key = 'last_printer', size = (10,1))
rejected_carts = sg.Input(key = 'rejected_carts', size = (20, 1))
notes_for_self = sg.Input(key = 'notes_for_self', size = (50,4))
notes_for_ops = sg.Input(key = 'notes_for_ops', size = (50, 4))
notes_for_escalation = sg.Input(key = 'notes_for_escalation', size =(50,4))
layout = [
[sg.Text("Last Printer:"), last_printer],
[sg.Text("Rejected Cartridges:"), rejected_carts],
[sg.Text("Notes for Self:"), notes_for_self],
[sg.Text("Notes for Operators:"), notes_for_ops],
[sg.Text("Notes for Escalation:"), notes_for_escalation],
[sg.Submit(), sg.Cancel()]
]
window = sg.Window("Data Analysis Entry Form", layout) #, finalize=True, keep_on_top=True, grab_anywhere=False, size = (1220, 600))
event, values = window.read()
window.close()
print("event is: " + event)
for each in values.items():
print(each)
if __name__ == '__main__':
main()
last_printer = sg.Multiline(key = 'last_printer', size = (10,1))
rejected_carts = sg.Multiline(key = 'rejected_carts', size = (20, 1))
notes_for_self = sg.Multiline(key = 'notes_for_self', size = (50,4))
notes_for_ops = sg.Multiline(key = 'notes_for_ops', size = (50, 4))
Related
This is almost certainly, very trivial however im dumb so help.
import PySimpleGUI as sg
ChessMenuOption= sg.Button('',image_filename = r'C:\Users\benja\Documents\Python\Chess project\Chess_Selection_Image.png')
sg.Window(title = 'Chess', layout = [[ChessMenuOption]] , size = (800,800)).read()
This places the button at the topleft (0,0), how do I place it somewhere else?
import PySimpleGUI as sg
dummy_element = sg.Text('', size=(100, 100))
ChessMenuOption = sg.Button('', image_filename=r'C:\Users\benja\Documents\Python\Chess project\Chess_Selection_Image.png', button_color=('white', 'white'), pad=(0, 0), size=(800, 800))
layout = [[dummy_element, ChessMenuOption]]
window = sg.Window(title='Chess', layout=layout, size=(900, 900))
window.read()
Button cannot be moved in PySimpleGUI.
If your project is about the chess, you can use the Graph element, then you can move figures in the Graph.
Example Code
import base64
import io
from PIL import Image
import PySimpleGUI as sg
def rotate(data):
file = io.BytesIO(base64.b64decode(data))
im = Image.open(file)
im = im.transpose(Image.ROTATE_180)
with io.BytesIO() as output:
im.save(output, format="PNG")
data = output.getvalue()
return data
width, height = size = (640, 640)
dx, dy = width//8, height//8
ddx, ddy = (dx-56)//2, (dy-56)//2
layout = [
[sg.Graph(size, (0, 0), size, pad=(0, 0), background_color='green', enable_events=True, key='Graph')],
]
window = sg.Window('Chess', layout, margins=(0, 0), finalize=True)
graph = window['Graph']
for i in range(8):
for j in range(8):
graph.draw_rectangle(
((i*dx), (j+1)*dy),
((i+1)*dx, j*dy),
fill_color='white' if (i+j) % 2 else 'black')
figures = {}
for i, image in enumerate(sg.EMOJI_BASE64_HAPPY_LIST[:8]):
figure = graph.draw_image(data=image, location=(i*dx+ddx, dy-ddy))
figures[(i, 0)] = figure
figure = graph.draw_image(data=sg.EMOJI_BASE64_WIZARD, location=(i*dx+ddx, 2*dy-ddy))
figures[(i, 1)] = figure
for i, image in enumerate(sg.EMOJI_BASE64_SAD_LIST[:8]):
figure = graph.draw_image(data=rotate(image), location=(i*dx+ddx, height-ddy))
figures[(i, 7)] = figure
figure = graph.draw_image(data=rotate(sg.EMOJI_BASE64_JEDI), location=(i*dx+ddx, height-dy-ddy))
figures[(i, 6)] = figure
selected, rectangle = False, None
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == 'Graph':
x, y = values[event]
i, j = x//dx, y//dy
if selected:
if (i, j) not in figures:
figure = figures[(i0, j0)]
graph.relocate_figure(figure, i*dx+ddx, (j+1)*dy-ddy)
del figures[(i0, j0)]
i0, j0 = i, j
figures[(i0, j0)] = figure
else:
i0, j0 = i, j
graph.delete_figure(rectangle)
rectangle = graph.draw_rectangle((i0*dx, (j0+1)*dy), ((i0+1)*dx, j0*dy), line_color='yellow', line_width=5)
else:
if (i, j) in figures:
i0, j0 = i, j
rectangle = graph.draw_rectangle((i0*dx, (j0+1)*dy), ((i0+1)*dx, j0*dy), line_color='yellow', line_width=5)
selected = True
else:
if rectangle:
graph.delete_figure(rectangle)
window.close()
I'm trying to make a cube layout tool to name and add polyprimatives to my scene, then reference in assets later. I have been able to make a basic UI based on some tutorials I've watched, but I can't seem to figure out how to add a translate feature onto my UI. I have it formatted under location, but when it comes to making it work I am totally stumped, if anyone could help clarify what I need to fix it, it would be greatly appreciated!
Here's the code so far.
import maya.cmds as cmds
import random
#Creates the board to layout buildings, might put into UI later if I have time.
cmds.polyPlane(width=100, height = 100, name = 'Ground')
#Define class, put DL to avoid confusion between other potential window items.
class DL_Window (object):
#creates method to construct window, call function "self".
def __init__(self):
#Creating some attributes, like name, title, and size of the window as it will initially appear on the screen.
self.window = 'DL_Window'
self.title = "City Layout Creator"
self.size = (400,400)
# will close old window if it's still open
if cmds.window(self.window, exists = True):
cmds.deleteUI (self.window, window= True)
#creates the new window
self.window = cmds.window (self.window, title=self.title, widthHeight=self.size)
#adjusts all UI to fit in the column
cmds.columnLayout(adjustableColumn = True)
#Create a title, and seperator from the name of the UI, and the function of the UI
cmds.text(self.title)
cmds.separator(height = 20, width = 100)
#Some customizable widgets that can adjust name, location of building on map, subdivisions for extruding windows or doors, and lastly the button.
self.cubeName = cmds.textFieldGrp( label = 'Building Name:')
self.cubeSize = cmds.floatFieldGrp ( numberOfFields = 3, label = 'size:', value1=1, value2=1,value3=1 )
self.cubeLocation = cmds.floatFieldGrp (numberOfFields = 3, label = 'location:', value1=1,value2=1,value3=1)
self.cubeSubdivs =cmds.intSliderGrp(field=True, label = 'subdivs', minValue=1,maxValue=20, value=1)
self.cubeCreateButton = cmds.button(label= 'Create Building', command=self.createBuilding)
#Repeat Steps for Trees
cmds.separator(height = 20, width = 100)
cmds.text("Tree Generator")
cmds.separator(height = 20, width = 100)
self.treeName = cmds.textFieldGrp( label = 'Tree Name:')
self.treeSize = cmds.floatFieldGrp ( numberOfFields = 3, label = 'size:', value1=1, value2=1,value3=1 )
self.treeLocation = cmds.floatFieldGrp (numberOfFields = 3, label = 'location:', value1=0,value2=0,value3=0)
self.treeSubdivs =cmds.intSliderGrp(field=True, label = 'subdivs', minValue=1,maxValue=20, value=1)
self.cubeCreateButton = cmds.button(label= 'Create Tree', command=self.createBuilding)
# Displays the window in Maya
cmds.showWindow()
def createBuilding (self, *args):
print ("A button has been pressed")
name = cmds.textFieldGrp(self.cubeName, query = True, text=True)
width = cmds.floatFieldGrp (self.cubeSize, query=True, value1=True)
height = cmds.floatFieldGrp (self.cubeSize, query=True, value2=True)
depth = cmds.floatFieldGrp (self.cubeSize, query=True, value3=True)
transformX = cmds.floatFieldGrp (self.cubeLocation, query = True, value1=True)
transformY = cmds.floatFieldGrp (self.cubeLocation, query = True, value2=True)
transformZ = cmds.floatFieldGrp (self.cubeLocation, query = True, value3=True)
subdivs = cmds.intSliderGrp (self.cubeSubdivs, query=True, value=True)
cmds.polyCube(name = name, width = width, height = height, depth = depth, subdivisionsWidth = subdivs,subdivisionsHeight = subdivs,subdivisionsDepth = subdivs)
# Calling the class here
myWindow = DL_Window()
You could try it by simply moving the object:
pc = cmds.polyCube(name = name, width = width, height = height, depth = depth, subdivisionsWidth = subdivs,subdivisionsHeight = subdivs,subdivisionsDepth = subdivs)
cmds.move(transformX, transformY, transformZ, pc)
btw. you can simplyfy your floatFieldGrp querys a bit like this:
width, depth, height = cmds.floatFieldGrp (self.cubeSize, query = True, value=True)
transformX,transformY,transformZ = cmds.floatFieldGrp (self.cubeLocation, query = True, value=True)
I am constructing a messaging application, and using Tkinter for GUI. I am trying to create some buttons, but I receive an error message:
Traceback (most recent call last):
File "/home/artur/Documents/MScProject/MSc Project/Task #179276/main_program.py", line 174, in <module>
app = Application(the_window)
File "/home/artur/Documents/MScProject/MSc Project/Task #179276/main_program.py", line 49, in __init__
self.AES_radiobutton = Radiobutton(text = 'AES algorithm', bg = color, variable=self.var, value=0)
NameError: name 'Radiobutton' is not defined
I am posting all of the source code here:
import tkinter as tk
from tkinter import IntVar
from tkinter import messagebox
from tkinter import Frame
from tkinter import Frame
from tkinter import Text
from tkinter import Label
from AESEncDec import *
from MD5Hashing import *
from RSAEncDec import *
color = 'lightblue' #color our background
class Application(Frame):
def __init__(self, root=None):
Frame.__init__(self, root)
self.frame_width = 700
self.frame_height = 400
# Set configuration our frame
self.config(width = self.frame_width, height = self.frame_height, bg = color)
self.pack()
# Create textBox for input data
self.textbox_one = Text()
self.textbox_one.place(x = 30, y = 170, height = 200, width = 300 )
# Create textBox for result
self.textbox_two = Text()
self.textbox_two.place(x = 370, y = 170, height = 200, width = 300 )
label_input_text = Label( text = "Input text: ", bg = color)
label_input_text.place(x = 30, y = 155, height = 10, width = 70 )
label_output_text = Label( text = "Result: ", bg = color)
label_output_text.place(x = 370, y = 155, height = 10, width = 50 )
# IntVar help to detect, what radioButton was chosen
self.var = IntVar()
# Create radioButton for AES
self.AES_radiobutton = Radiobutton(text = 'AES algorithm', bg = color, variable=self.var, value=0)
self.AES_radiobutton.place(x = 100, y = 20, height = 30, width = 100 )
# Create radioButton for DSA
self.DSA_radiobutton = Radiobutton(text = 'DSA algorithm', bg = color, variable=self.var, value=1)
self.DSA_radiobutton.place(x = 100, y = 70, height = 30, width = 100 )
# Create radioButton for Hash function
self.HF_radiobutton = Radiobutton(text = 'Hash function', bg = color, variable=self.var, value=2)
self.HF_radiobutton.place(x = 100, y = 120, height = 30, width = 100 )
# Create label
self.lable_for_ask_bits = Label(text = 'Input size of bits:', bg = color)
self.lable_for_ask_bits.place(x = 210, y = 70, height = 30, width = 100 )
# Create textBox for input bits
self.input_bits = Text()
self.input_bits.place(x = 310, y = 75, height = 20, width = 50 )
self.input_bits.insert(INSERT, '16')
# Create button to encrypt text
self.encrypt_button = Button(root, text = "Encrypt text", command = self.encrypt_text)
self.encrypt_button.place(x = 420, y = 20, height = 80, width = 100 )
# Create button to decrypt text
self.decrypt_button = Button(root, text = "Decrypt text", command = self.decrypt_text)
self.decrypt_button.place(x = 540, y = 20, height = 80, width = 100 )
# Create button to hash
self.hash_button = Button(root, text = "Hash text", command = self.hashing )
self.hash_button.place(x = 420, y = 120, height = 30, width = 220)
# Create AES object, keyword "this is a very strong key"
# You can change keyword
self.AES = AESEncDec('this is a very strong key')
# Save bits
self.bit_length = 16
# Create RSA object
self.RSA = RSAEncDec(self.bit_length)
def encrypt_text(self):
self.textbox_two.delete("1.0", END)
# Get radioButton selection
selection = self.var.get()
# if chosen AES
if selection == 0:
# Read text from input
message = self.textbox_one.get("1.0", END)
encrypt_message = self.AES.encrypt(message)
# Output result
self.textbox_two.insert(INSERT, encrypt_message)
# if chosen RSA
elif selection == 1:
try:
# Read number of bits
tmp_bits = int(self.input_bits.get("1.0", END))
# if bits not in range from 4 to 32 print error message
if tmp_bits < 4 or tmp_bits > 32:
tkMessageBox.showerror(message ='Bits must be in range from 4 to 32')
else:
# else, if tmp_bits not = self.bit_length: create new object
if tmp_bits != self.bit_length:
self.bit_length = tmp_bits
self.RSA = RSAEncDec(self.bit_length)
except:
tkMessageBox.showerror(message ='You must input integer number')
# Find max number
max_number = self.RSA.get_max_value_to_encrypt()
try:
# Read text from input (myst be number)
message = int(self.textbox_one.get("1.0", END))
if message < 0 or message > max_number:
tkMessageBox.showerror(message ='Input text must be number in range from 0 to ' + str(max_number))
else:
encrypt_message = self.RSA.encrypt(message)
# Output result
self.textbox_two.insert(INSERT, encrypt_message)
except:
tkMessageBox.showerror(message ='Input text must be number in range from 0 to ' + str(max_number))
else:
tkMessageBox.showinfo(message ='You must select "AES" or "RSA" radioButton')
def decrypt_text(self):
self.textbox_two.delete("1.0", END)
# Get radioButton selection
selection = self.var.get()
# if chosen AES
if selection == 0:
# Read text from input
message = self.textbox_one.get("1.0", END)
decrypt_message = self.AES.decrypt(message)
# Output result
self.textbox_two.insert(INSERT, decrypt_message)
elif selection == 1:
# Read text from input
message = int(self.textbox_one.get("1.0", END))
decrypt_message = self.RSA.decrypt(message)
# Output result
self.textbox_two.insert(INSERT, decrypt_message)
else:
tkMessageBox.showinfo(message ='You must select "AES" or "RSA" radioButton')
def hashing(self):
# Get radioButton selection
selection = self.var.get()
# if chosen Hash function
if selection == 2:
# Read text from input
message = self.textbox_one.get("1.0", END)
# Hashing
hashing_message = Hashing(message)
# Output result
self.textbox_two.insert(INSERT, hashing_message)
else:
tkMessageBox.showinfo(message ='You must select "Hash function" radioButton')
#create object TK class
the_window = tk.Tk(className = " Cryptographic")
#create object Application
app = Application(the_window)
#run our Application
app.mainloop()
I still see the GUI though and an input box, but a lot of elements are missing. What might be the problem here?
Simple import of Radiobutton module solved the issue
from tkinter import *
from random import *
from functools import partial
class Game:
def __init__(self):
self.root = Tk()
self.frame = Frame(width = 574, height = 574)
self.frame.grid(columnspan = 30, rowspan = 30)
self.minex = []
self.miney = []
self.clickx = 0
self.clicky = 0
blank = PhotoImage(file = 'C:\\Users\\PC\\Desktop\\Python Programs\\Minesweeper\\blank.gif')
for i in range(0,30):
for j in range(0,30):
button = Button(width = 15, height = 15, padx = 2, pady = 2, image = blank, command = partial(self.click, j, i))
button.grid(row = i, column = j)
self.mine_place()
self.root.mainloop()
def mine_place(self):
for i in range(0,15):
self.minex.append(randint(1,30))
self.miney.append(randint(1,30))
def click(self, j, i):
miss = PhotoImage(file = 'C:\\Users\\PC\\Desktop\\Python Programs\\Minesweeper\\miss.gif')
hit = PhotoImage(file = 'C:\\Users\\PC\\Desktop\\Python Programs\\Minesweeper\\hit.gif')
for k in range(0, len(self.minex)):
if j + 1 == self.minex[k] and i + 1 == self.miney[k]:
button = Button(image = hit)
button.grid(row = i, column = j)
else:
button = Button(image = miss)
button.grid(row = i, column = j)
app = Game()
In self.click, when I wish to create a button with this image I am given a blank image. If I create a button in init, the image comes out just fine. What is wrong?..............................................................
It looks like you're images are getting garbage collected you need to save a reference to the images after using PhotoImage.
ie - you create the image blank so save a reference as self.blank= blank and use image = self.hit
I'm writing a program that draws a rectangle or oval in a top frame depending on whether or not the user selects it via a radiobutton. There is a check button that determines whether the oval is filled as well. Both buttons are on the bottom frame. But for some reason when I run the code, it displays the window, but not the buttons themselves. How do I fix this?
Here's my code:
from tkinter import *
class GeometricFigures:
def __init__(self):
self.window = Tk()
self.window.title("Radiobuttons and Checkbuttons")
self.canvas = Canvas(self.window, width = 300, height = 100, bg = "white")
self.canvas.pack()
def drawButtons(self):
self.bottomframe = Frame(self.window)
self.bottomframe.pack()
self.check = IntVar()
cbtFilled = Checkbutton(self.bottomframe, variable = self.check, value = 0,
text = "Filled", command = self.processCheckbutton).pack(side = LEFT)
self.radio = IntVar()
rbRectangle = Radiobutton(self.bottomframe, variable = self.radio, value = 1,
text = "Rectangle", command = self.processRadiobutton.pack())
rbOval = Radiobutton(self.bottomframe, text = "Oval", variable = self.radio,
value = 2, command = self.processRadiobutton.pack())
cbtFilled.grid(row = 1, column = 2)
rbRectangle.grid(row = 1, column = 3)
rbOval.grid(row = 1, column = 4)
def processCheckbutton(self):
print("The check button is " +
("checked " if self.check.get() == 1 else "unchecked"))
def processRadiobutton(self):
print(("Rectangle" if self.radio.get() == 1 else "Oval")
+ " is selected ")
def drawRect(self):
self.canvas.create_rectangle(30, 10, 270, 60, tags = "rect")
def drawFillOval(self):
self.canvas.create_oval(30, 10, 270, 60, fill = 'blue', tags = "oval")
def drawOval(self):
self.canvas.create_oval(30, 10, 270, 60, tags = "oval")
def main(self):
test = GeometricFigures()
if self.check.get() == 1:
test.drawFillOval()
if self.radio.get() == 1:
test.drawRect()
else:
test.drawOval()
test.drawButtons()
if __name__ == '__main__':
main()
Thanks!