How to move an image with keyboard in Kivy? - keyboard-events

I am simply trying to move an image from left to right using the keyboard keys. I tried making a class called movableImage that inherits from Image. I think this is where I did something wrong, specifically the init function. When I run the code below I am getting AttributeError: 'function' object has no attribute 'widget' on line 16. What am I doing wrong here?
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.image import Image
from kivy.input.motionevent import MotionEvent
from kivy.core.window import Window
class character(Widget):
pass
class moveableImage(Image):
def __init__(self, **kwargs):
super(moveableImage, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard
if self._keyboard.widget:
# If it exists, this widget is a VKeyboard object which you can use
# to change the keyboard layout.
pass
self._keyboard.bind(on_key_down=self._on_keyboard_down)
def on_keyboard_down(self, keyboard, keycode, text, modifiers):
if keycode[1] == 'left':
print keycode #move littleguy to the left
elif keycode[1] == 'right':
print keycode #move littleguy to the right
return True
littleguy = moveableImage(source='selectionscreen/littleguy.zip', anim_available=True, anim_delay=.15)
class gameApp(App):
def build(self):
m = character()
m.add_widget(littleguy)
return m
if __name__ == '__main__':
gameApp().run()
I should also add that I HAVE read the Kivy keyboardlistener example and I am still stuck.

Here's a working example of what you're trying to do, just run it and use the right / left arrow keys to move it right / left:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.image import Image
from kivy.core.window import Window
class character(Widget):
pass
class MoveableImage(Image):
def __init__(self, **kwargs):
super(MoveableImage, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard(None, self)
if not self._keyboard:
return
self._keyboard.bind(on_key_down=self.on_keyboard_down)
def on_keyboard_down(self, keyboard, keycode, text, modifiers):
if keycode[1] == 'left':
self.x -= 10
elif keycode[1] == 'right':
self.x += 10
else:
return False
return True
class gameApp(App):
def build(self):
wimg = MoveableImage(source='tools/theming/defaulttheme/slider_cursor.png')
m = character()
m.add_widget(wimg)
return m
if __name__ == '__main__':
gameApp().run()
The problem you ran into, is that request_keyboard is a function, and needs to be called that way. You can also remove the if self._keyboard.widget: part.

Related

Copy file instantly while Tkinter Program is running

I have a little GUI Program that should only get some inputs to use these inputs for a method call.
Therefore I have several tk.Entry objects. Now my method that I want to execute needs a file from my hard disk. I want to scrape it with ElementTree. (I think the file needs to be in the same directory as my .py file? If not please tell me how to use a file from another path). My Idea was to copy that file into the current filepath and then only use a simple call to get access to this file.
But when I want to copy this file it is getting copied after I close the program and that should not happen. It should instantly copy the file so I can work with it.
Is there a way to refresh my files or do you have another idea?
Thank you and here is my code:
from Modules import copy_file
import tkinter as tk
class MainApplication(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
for Frame in (StartPage, SuccessPage):
page_name = Frame.__name__
frame = Frame(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("StartPage")
# method to show frames
def show_frame(self, page_name):
frame = self.frames[page_name] #getting our frame from our frames list
frame.tkraise() #raise it up to the front
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
# Open File Button
self.button_open_file = ttk.Button(self, text = "Open File", command = self.get_file_directory).grid(row=5, column=0, sticky="WE")
def get_file_directory(self):
filename = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("all files","*.*"),("jpeg files","*.jpg")))
copy_file(filename)
class SuccessPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
# Open File Button
self.label = tk.Label(self, text = "Lab").grid(row=0 column=0, sticky="WE")
if __name__ == '__main__':
application = MainApplication().mainloop()
----------------------------------------------------------------------------------------------
Modules.py
from shutil import copy
def copy_file(src):
destination_path = os.path.dirname(os.path.realpath(__file__))
copy(src, destination_path)

Is there a way to use/get the value from a current entry window

I'm trying to get the variable that's entered in an entry widget on the Return key pressed event, but struggling a bit. What I have tried has always produced a blank result.
This code may look messy and hap-hazard, but it's only going to be a template that I'll be using on a current project!
I've tried that many things to get it to work, I can't remember what I have tried!
from collections import OrderedDict
try:
import tkinter as tk
except:
import Tkinter as tk
root = tk.Tk()
labelLIST = OrderedDict([
('Temp ID', 'tempID'),
('PO Number', "poNumber"),
('Reference', "reference"),
('Cut/Sample Date', "csDate"),
('Cut Number', "cut")
])
i = 0
e_loops = len(labelLIST)
print (e_loops)
def bval1(event=None):
for i in range(e_loops):
print (entries[i].get())
entries[0].delete(0, tk.END)
entries[0].insert(0, 'DISABLED')
entries[0].configure(state='disabled')
def bval2():
entries[0].configure(state='normal')
for i in range(e_loops):
entries[i].delete(0, tk.END)
entries[0].focus()
def onClick(event):
ent = event.widget # event.widget is the widget that called the event
print(ent.cget("text")) # Print the text for the selected button
event.widget.tk_focusNext().focus()
def enterEV(event):
# print(entries[].get())
event.widget.tk_focusNext().focus()
entries = []
for key, value in labelLIST.items():
label = tk.Label(root, text=key)
label.grid(row=i, column=0, sticky="ew", padx=1, pady=1)
entry = tk.Entry(root, width=10)
entry.grid(row=i, column=1, sticky="ew", padx=5, pady=5)
if value == "cut":
entry.bind('<Return>', bval1)
else:
# entry.bind('<Return>', enterEV)
entry.bind('<Return>', onClick)
entries.append(entry)
i = i+1
button = tk.Button(root, text="Submit", command=bval1)
button.grid(row=0, column=2, columnspan=9, sticky="ew")
button = tk.Button(root, text="Clear", command=bval2)
button.grid(row=1, column=2, columnspan=9, sticky="ew")
entries[0].focus()
tk.mainloop()
When enter/return is pressed, I want the value that is the entry box to be printed to terminal via the onClick event. But the output is always empty.
def onClick(event):
ent = event.widget # event.widget is the widget that called the event
print(ent.cget("text")) # Print the text for the selected button
event.widget.tk_focusNext().focus()
You don't use the text attribute to get the value in an Entry widget. Using cget("text") returns the value for the textvariable attribute. Since you haven't set that attribute, it will always be the empty string.
Instead, you need to call the get method:
print(ent.get())

Maya Python static class function scope

I have some trouble getting my static class to work. There is something I am missing about the scope of functions within a class. If called the script gives me following error:
NameError: global name 'disableCostumFrames' is not defined #
import maya.cmds as cmds
from functools import partial
class Blast:
def createWindow():
# Todo:
# hanldes the gui for the user
windowID = 'window'
if cmds.window(windowID, exists = True):
cmds.deleteUI('window')
window = cmds.window(windowID, title="Blast", iconName='Blast', widthHeight=(400, 200) )
cmds.frameLayout( label='')
cmds.rowColumnLayout( numberOfColumns=4, columnWidth=[(1, 100),(3, 100)] )
cmds.text( label='Start: ' )
global Blast_startFrame
Blast_startFrame = cmds.textField( enable = False)
cmds.text( label=' End: ' )
global Blast_endFrame
Blast_endFrame = cmds.textField( enable = False)
cmds.setParent('..')
cmds.rowColumnLayout( numberOfColumns=2, columnWidth=[(1, 100), (2, 100)] )
cmds.radioCollection()
#cmds.radioButton( label='Full', select = True, onCommand= partial(disableCostumFrames, Blast_startFrame, Blast_endFrame ) )
#cmds.radioButton( label='Costum', onCommand= partial(enableCostumFrames, Blast_startFrame, Blast_endFrame ) )
cmds.setParent('..')
cmds.rowColumnLayout( numberOfColumns=1, columnWidth=[(1, 400), (2, 100)] )
cmds.button( label='Playblast' ,command= 'createPlayblast')
cmds.setParent('..')
cmds.showWindow( window )
return Blast_startFrame, Blast_endFrame
def main():
createWindow()
def enableCostumFrames(Blast_startFrame, Blast_endFrame, *args):
cmds.textField(Blast_startFrame, edit=True, enable=True)
cmds.textField(Blast_endFrame, edit=True, enable=True)
def disableCostumFrames(Blast_startFrame, Blast_endFrame, *args):
cmds.textField(Blast_startFrame, edit=True, text="", enable=False)
cmds.textField(Blast_endFrame, edit=True, text="", enable=False)
How do I need to define these functions within the class? I am calling the module like that:
import sys
Dir = 'c:/Blast'
if Dir not in sys.path:
sys.path.append(Dir)
try: reload(Blast_v011)
except: import Blast_v011
Blast_v011.Blast()
maybe iam doing something wrong on this side? Appreciated any help.
In this case you need to add a self reference to all of the methods in your class. The usual python class looks like this:
class MyClass(object):
def __init__(self):
self.variable = 123
def some_method(self):
print "my variable = ", self.variable
def some_other_method(self):
if self.variable > 1:
self.some_method()
The self reference in the member functions is how you get at class member variables and other functions -- it's python's way of referring to what other languages call this.
Instance methods can only be called on an instance (it's the instance that gets passed in as self). You can make a method that is called on the class itself -- rather than any particular instance of the class -- using the #classmethod decorator. Classmethods also take an argument, but instead of self it's a reference to the class. You use it the same way to get variables defined at the class level, which are shared by all copies of the class:
class HasShared(object):
shared = 99
#classmethod
def a_class_method(cls):
print cls.shared
(You can mix and match class and instance methods in the same class).
You can also make static methods using the #staticmethod decorator. These don't get a default argument at all:
class NotPythonic(object):
#staticmethod
def needs_no_args():
print "call me as NotPythonic.needs_no_args()"
In Python we tend to avoid this formula, since you can get a static method by just creating a function in a module without making a class to hold them. For the example code you posted, I'd probably just make a conventional class using instance methods, since your functions need the names of the gui widgets to be able to actually ask them questions.

PyQt iterate over a a QTreeView with checkbox

i am trying to make a UI that display folders from a given directory,
and the user can check or uncheck the folder.
i would like to get the information of what was checked and return the folder
i know i can loop / iterate in a QTableWidget, but if its a view, or its comming from a Qt.Dir how its done?
(even might be a easier way to add the check boxes to the dir view model than making another class)
thanks.
from PyQt4 import QtCore, QtGui
import sys
import argparse
def parseOpt():
parser = argparse.ArgumentParser(description="Check if the files in this folder are valid EXRs")
parser.add_argument("-file", dest="filepath", help="The file path to be checked.")
return parser.parse_args()
ARGS = parseOpt()
class CheckableDirModel(QtGui.QDirModel):
#a class to put checkbox on the folders
def __init__(self, parent=None):
QtGui.QDirModel.__init__(self, None)
self.checks = {}
def data(self, index, role=QtCore.Qt.DisplayRole):
if role != QtCore.Qt.CheckStateRole:
return QtGui.QDirModel.data(self, index, role)
else:
if index.column() == 0:
return self.checkState(index)
def flags(self, index):
return QtGui.QDirModel.flags(self, index) | QtCore.Qt.ItemIsUserCheckable
def checkState(self, index):
if index in self.checks:
return self.checks[index]
else:
return QtCore.Qt.Unchecked
def setData(self, index, value, role):
if (role == QtCore.Qt.CheckStateRole and index.column() == 0):
self.checks[index] = value
self.emit(QtCore.SIGNAL("dataChanged(QModelIndex,QModelIndex)"), index, index)
return True
return QtGui.QDirModel.setData(self, index, value, role)
#def filtering(self, index):
# self.checks.setFilter(QtCore.QDir.Dirs|QtCore.QDir.NoDotAndDotDot)
class Ui_Dialog(QtGui.QDialog):
def __init__(self,parent=None):
QtGui.QDialog.__init__(self,parent)
self.setObjectName("Dialog")
self.resize(600, 500)
self.llayout = QtGui.QVBoxLayout(parent)
self.model = CheckableDirModel()
self.model.setFilter(QtCore.QDir.Dirs|QtCore.QDir.NoDotAndDotDot)
#self.tree = QtGui.QTreeWidget()
self.tree = QtGui.QTreeView()
self.tree.setModel(self.model)
self.tree.setSortingEnabled(True)
self.tree.setRootIndex(self.model.index(ARGS.filepath))
#self.tree.hideColumn(1)
#self.tree.hideColumn(2)
#self.tree.hideColumn(3)
self.tree.setWindowTitle("Dir View")
self.tree.resize(400, 480)
self.tree.setColumnWidth(0,200)
self.but = QtGui.QPushButton(QtCore.QString("Run"))
self.llayout.addWidget(self.tree)
self.llayout.addWidget(self.but)
self.setLayout(self.llayout)
self.but.clicked.connect(self.print_path)
def print_path(self):
print "hello"
root = self.tree.childCount()
print root
for i in range(root):
print i.text()
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
ui = Ui_Dialog()
ui.show()
sys.exit(app.exec_())
If I understand you correctly, what you want is to replace your print_path method with the following:
def print_path(self):
print "hello"
for index,value in self.model.checks.items():
if value.toBool():
print self.model.filePath(index)

Why does the third access to this array return null?

I'm writing tetris in Scala as an exercise and I came around this strange behavior:
abstract class Orientation(protected val index: Int) {
private[Orientation] val impls: Array[Orientation] = Array(OrientedLeft, OrientedUp, OrientedRight, OrientedDown)
def rotateCounterClockwise = impls(
if (index == 0) 3
else index - 1
)
def rotateClockwise = impls((index + 1) % 4)
}
object OrientedLeft extends Orientation(0) {
override def toString = "Left"
}
object OrientedUp extends Orientation(1) {
override def toString = "Up"
}
object OrientedRight extends Orientation(2) {
override def toString = "Right"
}
object OrientedDown extends Orientation(3) {
override def toString = "Down"
}
object Test extends Application {
var orientation: Orientation = OrientedUp
for (i <- 0 until 20) {
println("Oriented to: "+ orientation)
orientation = orientation.rotateClockwise
}
}
Running Test gives this output:
Oriented to: Up
Oriented to: Right
Oriented to: Down
Oriented to: null
Followed by an java.lang.NullPointerException obviously. I mean: What the hell is going on here?
Just move impls in a companion object:
object Orientation {
private val impls: Array[Orientation] = Array(OrientedLeft, OrientedUp, OrientedRight, OrientedDown)
}
abstract class Orientation(protected val index: Int) {
import Orientation._
def rotateCounterClockwise = impls(
if (index == 0) 3
else index - 1
)
def rotateClockwise = impls((index + 1) % 4)
}
The reason why you had this error is that you have circular initializaiton dependencies: everytime you instantiate Orientation, you access the four Orientation singletons.
Simply put, accessing OrientedUp forces its instantiation, which in turn forces the instantian of all four singletons, including OrientedUp itself, which is still being constructed.
This is why you get null for this "still being constructed" value.
This is probably an initialisation problem. Each instance of Orientation has an array of OrientedX-objects, who are themselves instances of Orientation. Scala objects are initialised lazily, but that triggers population the impls array with objects that are currently being initialised. Hence the null entry.
Do what RĂ©gis suggested or mark val impls as lazy.

Resources