I want to be able to change C++ display text from an HTML txt file, is this possible to do?
your issue is your using pack and grid. pack and grid do the same thing but grid lets you choose where to put it. you can only use one or the other in a canvas. also you had hello and goodbye in the same spot on grid(). heres your fixed code:
from tkinter import *
from tkinter import ttk
label = None
def change1():
global label
label.config(text="Hello World!")
def change2():
global label
label.config(text="Goodbye World!")
def main():
global label
rootWindow = Tk()
label = ttk.Label(rootWindow, text="Hello World!")
label.grid(row=0, column=0)
button1 = ttk.Button(rootWindow, text="Hello!", command=change1)
button1.grid(row=0, column=1)
button2 = ttk.Button(rootWindow, text="Bye!", command=change2)
button2.grid(row=0, column=2)
rootWindow.mainloop()
main()
your problem is that you are using both .grid() and .pack() the difference is that in .grid you can choose where to put your button or whatever while in .pack() it places it automatically. this is why I will recommend you to use the .grid() option.
Related
I want to overwrite/replace the text Label with a button.
If I try to make a new label and place it at the same location, the previous text is still visable since it is longer than the new text.
Is it possible to delete the previous text first? any hints
root = tk.Tk()
tabControl = ttk.Notebook(root)
tab1 = ttk.Frame(tabControl)
def changetext():
labeltest = Label(tab1, text="short text").place(x=20, y=20)
labeltest = Label(tab1, text="long long long text").place(x=20, y=20)
button1 = Button(tab1, text="Change text pls", command=changetext)
button1.place(x=20, y=50)
root.mainloop()
Your code has this mistake. You shouldn't define a widget and pack/place/grid it on the same line. It's bad practise and you loose the reference to that widget. And as #acw1668 pointed out you can use <tkinter.Label>.config(text=<new text>) like this:
import tkinter as tk
def changetext():
labeltest.config(text="short text")
root = tk.Tk()
labeltest = tk.Label(root, text="long long long text")
labeltest.pack()
button1 = tk.Button(root, text="Change text pls", command=changetext)
button1.pack()
root.mainloop()
I am trying to display both an image and a box with an Entry widget. I can do that, but the window is so large that the widget at the bottom is mostly out of view. I have tried several calls to set the window's size or unmaximize it, but they seem to have no effect. I determined that the problem only occurs when the image is large, but still wonder how to display a large image in a resizable window or, for that matter, to make any changes to the window's geometry from code. All the function call I tried seem to have no effect.
Here is my code:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from gi.repository import GdkPixbuf
from urllib.request import urlopen
class Display(object):
def __init__(self):
self.window = Gtk.Window()
self.window.connect('destroy', self.destroy)
self.window.set_border_width(10)
# a box underneath would be added every time you do
# vbox.pack_start(new_widget)
vbox = Gtk.VBox()
self.image = Gtk.Image()
response = urlopen('http://1.bp.blogspot.com/-e-rzcjuCpk8/T3H-mSry7PI/AAAAAAAAOrc/Z3XrqSQNrSA/s1600/rubberDuck.jpg').read()
pbuf = GdkPixbuf.PixbufLoader()
pbuf.write(response)
pbuf.close()
self.image.set_from_pixbuf(pbuf.get_pixbuf())
self.window.add(vbox)
vbox.pack_start(self.image, False, False, 0)
self.entry = Gtk.Entry()
vbox.pack_start(self.entry, True,True, 0)
self.image.show()
self.window.show_all()
def main(self):
Gtk.main()
def destroy(self, widget, data=None):
Gtk.main_quit()
a=Display()
a.main()
Most of the posted information seems to pertain to Gtk2 rather than Gtk3, but there is a solution: to use a pix buf loader and set the size:
from gi.repository import Gtk, Gdk, GdkPixbuf
#more stuff
path = config['DEFAULT']['datasets']+'working.png'
with open(path,'rb') as f:
pixels = f.read()
loader = GdkPixbuf.PixbufLoader()
loader.set_size(400,400)
loader.write(pixels)
pb = GdkPixbuf.Pixbuf.new_from_file(path)
self.main_image.set_from_pixbuf(loader.get_pixbuf())
loader.close()
Is there a way to easily change the button name in the ipywidgets module? I am using the decorator, but cannot find in the documentation how to change the name to something other than "Run Interact". I believe I need to use the decorator since my function needs to be run on demand and depends on multiple inputs from different widgets, but I'm open to other ways of doing so as well.
import ipywidgets as widgets
from IPython.display import display
#widgets.interact_manual(number1 = widgets.Dropdown(
options=[1,2],
description='select a number'),
number2 = widgets.Dropdown(
options=[3,4],
description='select another number'))
def add_numbers(number1,number2):
return number1+number2
A bit quick and dirty but you can set
widgets.interact_manual.opts['manual_name'] = 'Your text here'
before defining your function and this should change the label name. If you have multiple interact_manual calls that need different labels you will need to change it each time.
import ipywidgets as widgets
from IPython.display import display
widgets.interact_manual.opts['manual_name'] = 'Your text here'
#widgets.interact_manual(number1 = widgets.Dropdown(
options=[1,2],
description='select a number'),
number2 = widgets.Dropdown(
options=[3,4],
description='select another number'),)
def add_numbers(number1,number2):
return number1+number2
I'm having trouble creating widgets in a Jupyter notebook that update when other widget values are changed. This is the code I've been playing around with:
from ipywidgets import interact, interactive, fixed
import ipywidgets as widgets
from IPython.display import display
def func(arg1,arg2):
print arg1
print arg2
choice = widgets.ToggleButtons(description='Choice:',options=['A','B'])
display(choice)
metric = widgets.Dropdown(options=['mercury','venus','earth'],description='Planets:')
text = widgets.Text(description='Text:')
a = interactive(func,
arg1=metric,
arg2=text,
__manual=True)
def update(*args):
if choice.value == 'A':
metric = widgets.Dropdown(options=['mercury','venus','earth'],description='Planets:')
text = widgets.Text(description='Text:')
a.children = (metric,text)
else:
metric = widgets.Dropdown(options=['monday','tuesday','wednesday'],description='Days:')
text2 = widgets.Textarea(description='Text2:')
a.children = (metric,text2)
choice.observe(update,'value')
display(a)
The resulting widgets metric and text do change based whether A or B is selected, but the problem is that the "Run func" button goes away as soon as I change to B. I've tried adding the __manual attribute immediately before display(a), adding it within update, and several other places. How do I change the children of the widget box without overwriting the fact that I want to manually run the function?
I am trying to remove a label in tkinter, but I can't seem to get it to work.
self.label(text='message')
self.label.grid(row=1,column=1)
def removelabel(labelname):
labelname.grid_remove()
removelabel(self.label)
You have to save a reference to the widget, which requires to to create the widget and lay it out in two steps:
self.label = tk.Label(...)
self.label.grid(row=1, column=1)
...
def removelabel(label):
label.grid_remove()
...
removelabel(self.label)