I need to build multiple buttons, but I can't display any button! this is my code of a WPF application with IronPython 2.7..
I've tried this in two different ways, with "Grid.Children.Add(button)" and without that but anything displays..
XAML:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
x:Name="NewWindow" Height="564.22" Width="993.2">
<Grid x:Name="grid" Margin="0,0,75.2,92.4">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Content="Create a Button from code" HorizontalAlignment="Left"
Margin="712,418,0,0" VerticalAlignment="Top" Width="167" Height="61"
Click="CreateButtons_Click"/>
</Grid>
</Window>
CODE:
import clr
clr.AddReferenceToFileAndPath("IronPython.Wpf.dll")
clr.AddReferenceByPartialName("PresentationCore")
clr.AddReferenceByPartialName("PresentationFramework")
clr.AddReferenceByPartialName("WindowsBase")
clr.AddReferenceByPartialName("IronPython")
clr.AddReferenceByPartialName("Microsoft.Scripting")
import wpf
from System.Windows import Application, Window, Controls
from System.Windows.Forms import Form, Button
class NewWindow(Window):
def __init__(self):
wpf.LoadComponent(self, 'NewWindow.xaml')
def CreateButtons_Click(self, sender, e):
self.myButton = Button()
self.myButton.Name="boton1"
self.myButton.Text="prueba"
self.myButton.Size = Size(75,23)
self.myButton.Location = Point(0,0)
#Add to grid
self.grid.Children.Add(self.myButton)
That shows an error:
expected UIElement, got Button
If I skip that error and erase the line self.grid.Children.Add(self.myButton), it doesnt get an error, but doesnt show any button either.
How i can show my button in there by python code? (New at python and wpf)
I figured it out what was the problem!! (So easy i can't beieve it!)
The error of UIElement was because (I didnt know that there was two kind of buttons, im sorry!) I was importing Button from System.Windows.Forms and the correct button has to be imported from System.Windows.Controls
Nice day, everybody!
Related
In a window class I have this button...
encrypt_button = QPushButton("Generate")
encrypt_button.setProperty('class', 'warning')
encrypt_button.clicked.connect(
lambda: encrypt_message(encrypt_message_box.toPlainText(), encrypt_public_key_box.toPlainText(),
encrypt_output_box))
encrypt_button.setObjectName("encrypt_button")
The encrypt_message function opens a error QMessageBox if there are values missing...and all works fine.
But I've come to write a unittest and I cannot seem to test for it opening...when I simulate the button click the window actually opens and will not continue until I click OK
def test_copy(self):
""" checks to make sure the button doesn't copy when its blank """
for button in self.window.copy_buttons:
button.click()
##some test here
I've enlisted the help of our friendly AI helper, who keeps telling me to use QSignalSpy or qWaitForWindowExposed, but everything I try just results in the same thing. The QMessageBox opens and I'm stuck waiting for user input.
Any suggestions?
I am using a python install that doesn't accept Tkinter or any other gui library but will accept .Net and windows forms. I have a message box that needs to come on screen to give the user a message but it always comes up dead centre of the monitor. As the user needs to interact with the window directly underneath the message box the user needs to grab the message box and drag it to another part of the screen before interacting with the window underneath and then pressing ok on the message box. Is there any other way to set the message box to appear in a corner of the monitor or elsewhere?
EDIT: I have a form now, but still don't know the method to call to position the window elsewhere than centre.
Initial Code
import clr
clr.AddReference("System.Windows.Forms")
import System.Windows.Forms as WF
WF.MessageBox.Show("Message box in centre ")
Newer code - still doesn't get me a window in the corner
import sys
sys.path.append(r'C:\Python24\Lib')
import clr
clr.AddReference("System.Drawing")
clr.AddReference("System.Windows.Forms")
from System.Drawing import Point
from System.Windows.Forms import Application, Button, Form, Label
class SdnMessageForm(Form):
def __init__(self):
self.Text = 'SDN winform'
self.label = Label()
self.label.Text = "Move drops to datum"
self.label.Location = Point(50, 50)
self.label.Height = 30
self.label.Width = 200
self.CenterToScreen()
self.count = 0
button = Button()
button.Text = "OK"
button.Location = Point(50, 100)
button.Click += self.buttonPressed
self.Controls.Add(self.label)
self.Controls.Add(button)
def buttonPressed(self, sender, args):
self.Close()
form = SdnMessageForm()
Application.Run(form)
print(dir(form))
Result
Creates a message box in the centre of the display
Desired result
A message box which is displayed in a corner.
Acceptable but not desired result
Comment out the self.CenterToScreen() line, this seems to open the form towards the left gof the display, its not custom but its better than I had. I would still appreciate a custom solution where I can position the form on the right upper corner.
I'm trying to port a program I made with pygtk, it's a popup menu launched via global shortcut (using keybinder) to run specific programs and commands. There is no main window in this program, the point is have a simple, fast and light "launcher" available anywhere, whenever I need it.
The old menu.popup used to work even when using 0 as event.time (since keybinder doesn't give an event I'd request a time for), but now I'm getting this error:
Warning: The property GtkStatusIcon:stock is deprecated and shouldn't be used anymore. It will be removed in a future version.
self.icon.new_from_stock(Gtk.STOCK_OPEN)
This is an example I made up to show the problem:
from gi.repository import Gtk, Gdk
from gi.repository import Keybinder
menu_shortcut = "<Super>m"
class TestMenu:
def __init__(self):
self.menu = Gtk.Menu()
item = Gtk.MenuItem('various items')
self.menu.add(item)
item = Gtk.MenuItem('Quit')
item.connect('activate', Gtk.main_quit)
self.menu.append(item)
self.menu.show_all()
self.icon = Gtk.StatusIcon()
self.icon.new_from_stock(Gtk.STOCK_OPEN)
self.icon.set_tooltip_text('MyMenu')
self.icon.connect('popup-menu', self.popup, self.menu)
Keybinder.init()
Keybinder.bind(menu_shortcut, self.popup, 0, 0, self)
def popup(self, widget, button, time, menu):
self.menu.popup(None, None, None, None, button, time)
menu = TestMenu()
Gtk.main()
With this example I'm able to click the status icon and get the menu, but the keyboard shortcut just gives me the aforementioned error.
Note: the stock icon doesn't work, I'm still learning the new API.
I have a fairly simply PyQt question. (Python 3.4, PyQt 4.11.3, Qt 4.8.5) I built a very simple dialog using Qt Designer (Ui_Dialog). This object has a QPushButton, a QLineEdit, and a QListWidget. I wrote another object that inherits from Ui_Dialog, and sets up a returnPressed signal from QLineEdit that should add some text to the QListWidget. Unfortunately, this does not work.
Here's my code:
import sys
from PyQt4 import QtGui
from dialog import Ui_Dialog
class ImDialog(QtGui.QDialog, Ui_Dialog):
def __init__(self):
super(ImDialog, self).__init__()
self.setupUi(self)
self.lineEdit.returnPressed.connect(self.additem)
self.pushButton.clicked.connect(self.listWidget.clear)
def additem(self):
text = self.lineEdit.text()
print(text)
self.listWidget.insertItem(0, text)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
ui = ImDialog()
ui.show()
sys.exit(app.exec_())
The text in the line editor prints fine to the terminal, but it is not added to the listWidget.
Interestingly, if I comment out the sys.exit line and run this in an IPython terminal, I can add as much text as I like to the listWidget without a problem.
[In 1]: %run that_program.py
[In 2]: ui.listWidget.insertItem(0, "Test") # This works fine
If anyone has any suggestions to get this to work (outside IPython), I would appreciate the help. Thanks
There is only one button in your dialog, and so it will become the auto-default. This means that whenever you press enter in the dialog, the button will receive a press event, even if it doesn't currently have the keyboard focus.
So the item does get added to the list-widget - it's just that it then immediately gets cleared by the auto-default button.
To fix this, reset the auto-default like so:
self.pushButton.setAutoDefault(False)
(NB: you can also change this property in Qt Designer).
Just as some history, I have been using python for about 5 years now and have finally decided to make my first gui app in Glade.
I started with something basic, I have a button, a Gtkentry and gtktextview
This is what I am trying to accomplish:
on button press, take from the text from gtk.entry and have it appended to the gtk.textview
now the main problem I have is that I can not find descent documentation for how to use the widgets, and the examples I find on the Internet reference both a builder variation as well as another variation of glade project which I can only assume has been discontinued. I would like to learn how builder fits into the python / glade collaboration.
my code so far:
import gtk
import pygtk
def onDeleteWindow(self, *args):
Gtk.main_quit(*args)
def hello(button):
text_buffer.set_text(txtinput.get_text())
builder = gtk.Builder()
builder.add_from_file("dagui.glade")
handlers = {
"onDeleteWindow": gtk.main_quit,
"buttondown": hello
}
builder.connect_signals(handlers)
textarea = builder.get_object("textview1")
window = builder.get_object("window1")
txtinput = builder.get_object("entry1")
window.show_all()
gtk.main()
window.show_all()
gtk.main()
now this all works and pressing the button will print what ever is in the gtk.entry but I can not find how to append it to the textview. I also am not sure what to search for to find documentation, I tried "gtk builder gtk.textview" and pygtk build gtk.textview append" and all other variations.
Though knowing how to simply add the text to the text view would be great, having a link to somewhere where I can get in plain english how to use these widgets I would be forever great-full.
Frob the gtk.TextView, you need to get the gtk.TextBuffer by using the textview's buffer property.
From the textbuffer, you need to get the iterator that points to the end of the buffer with the get_end_iter method. With that iterator, and your text, you can use the textbuffer's insert method.
Edit: Since I don't have the dagui.glade file, I couldn't test it, but see the following code:
def hello(button):
global textarea, txtinput
buffer = textarea.get_property('buffer')
i = buffer.get_end_iter()
buffer.insert(i, txtinput.get_text())
# clear the input window after appending the text
txtinput.set_text('')
I figured it out, I have found out the the gtk.textview.get_buffer actually sets the buffer ID and then the textview.set_text(buffer) isall I needed.
here is the full working code, the glade is just a button, an entry and a textview:
#!/usr/bin/python
import os
import sys
import gtk
import pygtk
def onDeleteWindow(self, *args):
Gtk.main_quit(*args)
def hello(button):
textbuffer = textarea.get_buffer()
textbuffer.set_text(txtinput.get_text())
builder = gtk.Builder()
builder.add_from_file("dagui.glade")
handlers = {
"onDeleteWindow": gtk.main_quit,
"buttondown": hello
}
builder.connect_signals(handlers)
textarea = builder.get_object("textview1")
window = builder.get_object("window1")
txtinput = builder.get_object("entry1")
window.show_all()
gtk.main()
Use this to add text :
textarea.set_text('whatever you want')
and this for adding pango markup ( http://goo.gl/94Pkk ) :
textarea.set_markup('<span size="large>Example</span>')
Here's the documentation : http://python-gtk-3-tutorial.readthedocs.org/en/latest/index.html