I am using the following script :
header = self.document.add_paragraph(style='Heading 1')
header.style.font.name = 'Arial'
header.style.font.size = Pt(16)
header.add_run('Header One')
The result is that "Header One" get 'Calibri'.
This is a legitimate bug even with python-docx version 0.8.5. If you were to change the font name of the style 'Normal', it works (as shown in the examples on the python-docx manuals), but this does not work for the 'Heading 1' style.
One workaround is to create a new heading style that takes Heading 1 as a base style and then modify the new style's font name & size:
from docx.enum.style import WD_STYLE_TYPE
styles = self.document.styles
new_heading_style = styles.add_style('New Heading', WD_STYLE_TYPE.PARAGRAPH)
new_heading_style.base_style = styles['Heading 1']
font = new_heading_style.font
font.name = 'Arial'
font.size = Pt(16)
self.document.add_paragraph('Header One', style='New Heading')
Related
I'm programming a Team Creator in which the user can write the name of the player and his strength. (So assuming the user writes 10 Players there are 5 Teams with 2 players each generated. I don't know how to store the values the users writes in the input (The values have to be stored since the input is empty again when the user presses the enter button in order to add the next player). I have already initialized the variables and the list but Im stuck when it comes to store the values.
My existing Elm Code:
import Browser
import Html exposing (Html, Attribute, button, text, h1, div, input, text)
import Html.Attributes exposing (style)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput)
-- MAIN
main =
Browser.sandbox { init = init, update = update, view = view }
-- MODEL
type alias Player =
{ player : String
, strength : Int
}
type alias Model =
{ content : String
, teams : List Player
}
init : Model
init =
{ content = ""
, teams = []
}
-- UPDATE
type Msg
= Change String
update : Msg -> Model -> Model
update msg model =
case msg of
Change newContent ->
{ model | content = newContent }
-- VIEW
view : Model -> Html Msg
view model =
div []
[ h1 [style "font-family" "impact"] [ text "Team Creator" ]
, div[] [ input [ placeholder "🏅 Team 1", style "width" "300px", style "height" "30px", style "font-size" "25px", style "color" "#488aff"] [] ]
, input [ placeholder "💪🏼 Strength", style "width" "300px", style "height" "30px", style "font-size" "25px", style "color" "#32db64"] []
, div [] [ button [ style "background-color" "#66cc81", style "color" "white", style "margin-top" "20px", style "width" "300px", style "border-radius" "25px", style "height" "60px", style "font-size" "30px", style "margin-right" "70px"] [ text "+ADD" ] ]
]
In Elm, if you want to capture the values of inputs, you need to use onInput for each field you care about. These will send a message containing the text to update each time the field changes. You'll end up creating one message per field that updates your model with the new value.
Then, when the user clicks a button to submit — use onClick to handle this — the update function should convert and validate those values stored in model. If they're good, make them into a Player and push it into the model.teams list!
From here, you could modify it to give feedback about errors when submitting, or even in realtime. But focus on getting the above working first! Refactoring is cheap in Elm.
The official guide has a section on the Elm Architecture and how buttons, text fields, and forms work within it. It's a good read that should hopefully clarify how all of these messages get sent out and flow through your program.
I've just started using R-Shiny. But I have some of troubles using js and html code in Shiny.
In my app I have two bsButton which when hover show some text with bsPopover. One of these popover contains an image which is larger than the standard box of the popover and I'd like to set the width of this popover that fully contains the figure.
Here I found how to set the width and height of all popovers, but how can I set the width/height of only a specific popover?
This is my code so far and I'd like to change the width of bsPopover(id="q2", ...) but not the width of bsPopover(id="q1", ...):
library(shiny)
library(shinyBS)
ui <- fluidPage(
tags$head(
# this changes the size of the popovers
tags$style(".popover{width:200px;height:250px;}")
),
fluidRow(
fileInput("file", label=h4("Upload Data",
tags$style(type = "text/css", "#q1 {vertical-align: top;}"),
bsButton("q1", label="", icon=icon("question"), style="info", size="extra-small")),
accept=".txt"
),
bsPopover(id="q1", title="Title help text1",
content=paste("help text"),
placement = "right",
trigger = "hover",
options = list(container = "body")
),
numericInput("numIn", label = h4("Choose a value",
tags$style(type="text/css", "#q2 {vertical-align: top;}"),
bsButton("q2", label="", icon=icon("question"), style="info", size="extra-small")),
value = 2.5, step=0.5),
bsPopover(id="q2", title="Title help text 2",
content=paste0("The figure below shows",
img(src='scenarios.png', align = "center", height="320px", width="800px", style="display:block;margin-top:20px;margin-left:auto;margin-right:auto;")
),
placement = "right",
trigger = "hover",
options = list(container = "body")
)
)
)
server <- function(input, output) {}
shinyApp(ui = ui, server = server)
I've been trying to solve the same issue, although using tipify() and popify() from the same shinyBS package. I found it really hard as it seems that dimensions of a tooltip or a pop-up are defined by the outer <div> container that tooltip or pop-up belongs to. Hence requires manipulation with CSS to resolve.
I found tippy::tippy() helpful here, where one could define the desired width of a tooltip inside a function, e.g.:
library(tippy)
tippy(
text = "Show tooltip over this text",
tooltip = "My tooltip text",
width = "200px"
)
The text argument requires character string as an input. That could be your button title/text.
I used tippy to show a tooltip over an information icon, that required minor CSS tweaking:
text = "<i class='fas fa-info-circle'></i>"
Just replace
tags$style(".popover{width:200px;height:250px;}")
with
tags$style("#q2{width:200px;height:250px;}")
I wanted to get user input from user using Tkinter's Entry something like this
from Tkinter import *
top = Tk()
label = Label(top, text="Enter your bio")
entry = Entry(top, bd = 2)
def create_new():
new_file = open('file.txt', 'w+')
user_input = str(entry) # I ALSO TRIED WITHOUT str()
new_file.write(user_input) #still doesn't work
button = Button(top, text = "SAVE", fg ="red", command=create_new)
label.pack()
entry.pack()
button.pack()
top.mainloop()
When I add my info in the field and hit SAVE, it does create a new file.txt but it doesn't write my info into the file.txt
file.txt only has some numbers like these
.22775808
.22710272
.22382592
etc...
Any ideas on how can I fix this? Also what do these numbers mean and why are they here?
from tkinter import *
top = Tk()
label = Label(top, text="Enter your bio")
entry = Entry(top, bd = 2)
def create_new():
new_file = open('file.txt', 'w+')
user_input = (entry).get() # I ALSO TRIED WITHOUT str()
new_file.write(user_input) #still doesn't work
button = Button(top, text = "SAVE", fg ="red", command=create_new)
label.pack()
entry.pack()
button.pack()`enter code here`
top.mainloop()
You aren't writing the contents of the Entry, you're writing the Entry itself - which from Python's point of view is just the randomly-generated name of the actual widget which lives in the embedded Tcl/Tk interpreter. Use entry.get() for the actual contents.
You're also forgetting to close the file after you write to it, so anything you do manage to write may not show up immediately.
I desire to change the description of textbox widgets depending on the chosen option of a dropdown widget. I thought I could achieve this by using a dictionary of lists. This is an example of what I did:
#Lists of text box descriptions
LEF_SOL = ['AMP','DEP','LAGTIME']
INI_SOL = ['AMP','DEP','XWavemaker']
INI_REC = ['Xc','Yc','WID']
# dictionary made with such lists
wave_options = {'left boundary solitary':LEF_SOL,
'initial solitary wave':INI_SOL,'rectangular hump':INI_REC}
#dropdown widget of the dictionary
wave_maker = widgets.Dropdown(options=wave_options)
# Textbox 1 description = first object of list depending on the dropdown
first_option = widgets.BoundedFloatText(width = "20%",height = '50px',
description = wave_maker.value[0])
# Textbox 2 description = second object of list depending on the dropdown
second_option = widgets.BoundedFloatText(width = "20%",height = '50px',
description = wave_maker.value[1])
# Textbox 3 description = third object of list depending on the dropdown
third_option = widgets.BoundedFloatText(width = "20%",height = '50px',
description = wave_maker.value[2])
display(wave_maker,first_option,second_option,third_option)
Yet when I run this, the textboxes descriptions stay being the ones belonging to the first option that appears in the dropdown. I want them to change if the dropdown is changed. I know that this can be done with 'link'. It should look something like this:
link((wave_maker,'value[0]'), (first_option, 'description'))
link((wave_maker,'value[1]'), (second_option, 'description'))
link((wave_maker,'value[2]'), (third_option, 'description'))
Yet an error occurs:
TypeError: <ipywidgets.widgets.widget_selection.Dropdown
object at 0x7fd47bbbd790> has no trait 'value[0]'
Yet when I change the 'link' to:
link((wave_maker,'value'), (first_option, 'description'))
then this error appears:
TraitError: The 'description' trait of a BoundedFloatText
instance must be a unicode string, but a value of ['AMP', 'DEP', 'XWavemaker']
<type 'list'> was specified.
Is there a way I could solve this?
I found that the solution to my question can be done by using the traitlets "observe" or "interact". I've attached an "observe" example:
wave_options = ('INI_SOL', 'WK_IRR')
int_range = widgets.Dropdown(options=wave_options)
Xc_WK = widgets.BoundedFloatText()
DEP_WK = widgets.BoundedFloatText()
display(int_range,Xc_WK,DEP_WK)
def on_value_change(value):
if int_range.value == 'WK_IRR':
Xc_WK.description = 'Xc_WK'
DEP_WK.description='DEP_WK'
else:
Xc_WK.description ='amp'
DEP_WK.description='DEP'
int_range.observe(on_value_change, 'value')
Recently I have been working on a GUI python plain text editor. The code calls this function:
def TimesNewRoman():
global fontname
global font
fontname = "Times New Roman"
print font
The variables are:
fontname = "Calibri"
size = "14"
font = fontname + " " + size
And Tkinter reads the font with the code:
textPad.config(
borderwidth=0,
font=font ,
foreground="green",
background="black",
insertbackground="white", # cursor
selectforeground="blue", # selection
selectbackground="#008000",
wrap="word",
width=64,
undo=True, # Tk 8.4
)
However, I cannot get it to work. I get no errors but the font remains Calibri. I have searched the internet looking for anything that might allow me to dynamically change the font of the text canvas, but I have not succeeded in finding one that works. Any help in implementing a font modifying feature would be very much appreciated.
I am using python 2.7.7, Tkinter, and I am running this on Windows 7.
Your function should change the font name to "Times New Roman". Are you sure you are calling the function?
Just for completeness, as also Bryan Oakley stated, you should use the tuple syntax when specifying a font name with more than one word (like I am doing in the example below).
If it's ok to dynamically change the font of the Text widget with the click of a button, then the following could be a simple solution that uses a Toplevel widget to let the user write the font and size:
import Tkinter as tk
def choose_font():
global m, text # I hate to use global, but for simplicity
t = tk.Toplevel(m)
font_name = tk.Label(t, text='Font Name: ')
font_name.grid(row=0, column=0, sticky='nsew')
enter_font = tk.Entry(t)
enter_font.grid(row=0, column=1, sticky='nsew')
font_size = tk.Label(t, text='Font Size: ')
font_size.grid(row=1, column=0, sticky='nsew')
enter_size = tk.Entry(t)
enter_size.grid(row=1, column=1, sticky='nsew')
# associating a lambda with the call to text.config()
# to change the font of text (a Text widget reference)
ok_btn = tk.Button(t, text='Apply Changes',
command=lambda: text.config(font=(enter_font.get(),
enter_size.get())))
ok_btn.grid(row=2, column=1, sticky='nsew')
# just to make strechable widgets
# you don't strictly need this
for i in range(2):
t.grid_rowconfigure(i, weight=1)
t.grid_columnconfigure(i, weight=1)
t.grid_rowconfigure(2, weight=1)
m = tk.Tk()
text = tk.Text(m)
text.pack(expand=1, fill='both')
chooser = tk.Button(m, text='Choose Font', command=choose_font)
chooser.pack(side='bottom')
tk.mainloop()
When you click Choose Font, another window shows up, where you can insert the font name and the font size. You can apply the new font name and font size through the click of another Button Apply Changes, which uses a lambda.
Note that I have not handled any possible wrong inputs (for example inserting a letter for the size), you can do it by your own.
The problem is how you are specifying the font. You should use a tuple rather than a string. Try this:
font = (fontname, size)
textPad.config(
...,
font=font,
...
)
A good place for tkinter documentation is effbot.org. On the page about widget styling it says this about specifying the font:
Note that if the family name contains spaces, you must use the tuple
syntax described above.