I am trying to update the label text after pressing a button. So far I’ve encountered this problem NameError: unknown option 'codelabel' for #<Tk::Button:0x00000000050146b8 @path=".w00003"> (deleted widget?)
.
I can’t seem to configure the already loaded label field in the GUI. I am told to use configure(‘text’=>‘new text’) but it doesn’t work, same as using label.text=‘new text’.
Here is my full code:
require ‘tk’
require ‘net/http’
#GET method
class Rest_client
attr_reader :code,:html
attr_accessor :code,:html
def initialize
@code=""
@html=""
end
# methods and code
def get_method (host, path)
# Create a URI
uri = URI.parse(host)
# Create HTTP object for the specified address and port
http = Net::HTTP.new(uri.host, uri.port)
# Create a GET request
request = Net::HTTP::Get.new(path)
# Perform request
reply = http.request(request)
# Print response status code
#puts "#{reply.code} #{reply.message}"
@code="#{reply.code} #{reply.message}"
# Loop through and print headers
#reply.header.each_header {|key,value| puts "#{key} = #{value}" }
# Print response body
#puts "#{reply.body}"
@html="#{reply.body}"
end
end
rc=Rest_client.new
#title
root = TkRoot.new { title "5.2 Task" }
#window size
root['geometry']='750x500'
#top side in-window opening title
TkLabel.new(root) {
text 'Ruby Restful Client'
pack { side 'top'}
}
#variables
txthost=TkVariable.new
txthost.value="Enter host here"
txtpath=TkVariable.new
txtpath.value="Enter path here"
txtcode=TkVariable.new
txthtml=TkVariable.new
#inputs
hostfield=TkEntry.new(root){
width 40
background "cyan"
foreground "red"
pack('side'=>'top','padx'=>10, 'pady'=>10)
}
hostfield.textvariable = txthost
pathfield=TkEntry.new(root){
width 40
background "cyan"
foreground "red"
pack('side'=>'top','padx'=>10, 'pady'=>10)
}
pathfield.textvariable = txtpath
TkButton.new(root){
text "GET"
#command proc { p txthost.value; p txtpath.value;exit }
command proc{
rc.get_method(txthost.value,txtpath.value);
txtcode.value=rc.code;
txthtml.value=rc.html;
codelabel.configure('text'=>txtcode.value)
}
pack('side'=>'bottom', 'padx'=>10, 'pady'=>10)
}
#label
codelabel=TkLabel.new(root){
width 45
background "cyan"
foreground "red"
pack('side'=>'top','padx'=>10, 'pady'=>10)
}
bodyfield=TkText.new(root){
background "cyan"
foreground "red"
pack('side'=>'top','padx'=>10, 'pady'=>10)
}
Tk.mainloop
Any advice on how to solve this issue?